【发布时间】:2017-04-30 09:35:07
【问题描述】:
好的,我将尽力解释我的项目是如何设置的,以便您可以适当地帮助我找出如何处理此配置。
我有一个parent component,即smart component。通过这个组件,可以访问我商店中的所有数据。
class DashboardPage extends React.Component {
componentDidMount() {
this.props.getTips();
}
render() {
return (
<div>
<div className="row">
<div className="col-sm-7">
<ContentBox
title="The Scoop"
footerText="Submit a Story"
showSlider
content={<TipOfTheDay tips={this.props.tips} />}
/>
</div>
</div>
</div>
)
}
}
DashboardPage.propTypes = {
getTips: PropTypes.func
}
function mapStateToProps(state, ownProps) {
tips: state.tips
}
function mapDispatchToProps(dispatch) {
return {
getTips: () => { dispatch(tipActions.loadTips());} ## This hits tipActions and runs the `action creator`: loadTips(). Which returns all tips from api.
}
}
export default connect(mapStateToProps, mapDispatchToProps)(DashboardPage);
如您所见,我在smart component、<ContentBox/> 和<TipOfTheDay/> 中包含了两个dumb components。在dashboardPage 上大约有7 个<ContentBox/> 组件,每个组件都继承了页眉/页脚的特殊标题,并且还通过showSlider 布尔值告知是否显示页脚。这是<ContentBox/> 的样子:
import React, {PropTypes} from 'react';
import Footer from './ContentBoxFooter';
const ContentBox = ({title, footerText, showSlider, content}) => {
return (
<div style={styles.root} className="col-sm-12">
<div style={styles.header} className="row">
<h3 style={styles.header.title}>{title}</h3>
<span style={styles.header.arrow} />
</div>
{content}
<Footer footerText={footerText} showSlider={showSlider} />
</div>
);
};
ContentBox.propTypes = {
title: PropTypes.string,
footerText: PropTypes.string,
showSlider: PropTypes.bool,
content: PropTypes.object
};
export default ContentBox;
这是页脚:
import React, {PropTypes} from 'react';
import styles from './contentBoxStyles';
import Previous from './svg/Previous';
import Next from './svg/Next';
import Add from './svg/Add';
import consts from '../../styles/consts';
const ContentBoxFooter = ({footerText, showSlider}) => {
if (footerText != undefined) {
return (
<div style={styles.footer} className="row">
{
showSlider ?
<div>
<Previous fillColor={consts.orange} height="20px" width="20px"/>
<span style={styles.bar}>|</span>
<Next fillColor={consts.orange} width="20px" height="20px"/>
</div> : <div style={styles.emptyArrow} />
}
<div style={styles.footer.link}>
<span style={styles.footer.link.text}>{footerText}</span>
<Add fillColor={consts.orange} height="24px" width="24px" />
</div>
</div>
);
} else {
return(null);
}
};
ContentBoxFooter.propTypes = {
footerText: PropTypes.string,
showSlider: PropTypes.bool
};
export default ContentBoxFooter;
很少!所以这里是我需要添加onClick 功能的地方。此功能需要添加到作为 SVG 的 <Previous/> 和 <Next/> 组件中。我试图做的是为我拉入的提示创建一个滑块。显然会有<Footer/> 组件需要相同的功能,但控制提示以外的不同数据。因为我是 React 和 Redux 的新手,所以我不知道如何执行此操作,而不仅仅是执行此操作,而是以“Redux”方式执行此操作。
如何让这两个嵌套在其他dumb components 中的svg 组件是dumb components,以对页面上的特定数据执行onClick 功能?我希望这是有道理的。为了更清楚,这是我对<TipOfTheDay/> 组件所做的事情:
const tipOfTheDay = ({tips}) => {
return (
<div style={styles.tipBody} className="row">
{
tips.map(function(tip, key) {
return (
<div key={key} className="myTips">
<h3 style={styles.tipBody.header}>{tip.title}</h3>
<p style={styles.tipBody.content}>{tip.content}</p>
</div>
);
})
}
</div>
);
};
tipOfTheDay.propTypes = {
tips: PropTypes.array.isRequired
};
export default tipOfTheDay;
感谢您随时阅读/回复/协助此问题。我是一个相当新的开发人员,这对我来说也是新技术。
【问题讨论】:
标签: javascript reactjs svg redux components