我很乐意学习redux并将其应用到我当前的项目中。在我的脑海里有一个问题。此刻动作是从道具传下来的。只是想知道我可以导入动作并调用动作吗?因为我可能有嵌套的嵌套组件保持传递下来的道具可能不理想会很好地触发深层次的动作而不传递道具下来。
任何建议表示赞赏:)
import React, { PropTypes, Component } from 'react';
export default class ButtonGroup extends Component{
static PropTypes = {
actions: PropTypes.object.isRequired
};
render() {
return (
<button className="button" onClick={ e => { this.props.actions.popForm()}}>Create New</button>
);
}
}
答案 0 :(得分:1)
为了完成您所描述的内容,您需要使用react-redux中的connect
。
所以,在你的例子中:
import React, { PropTypes, Component } from 'react';
import { connect } from 'react-redux';
import * as actions from './actions';
export class ButtonGroup extends Component{
static PropTypes = {
popForm: PropTypes.function.isRequired
};
render() {
return (
<button className="button" onClick={ e => { this.props.popForm()}}>Create New</button>
);
}
}
export default connect(null, actions)(ButtonGroup);