首页 > 解决方案 > React Redux - 另一个组件中的道具

问题描述

我正在尝试创建一个应用程序,我的应用程序的一部分类似于 Counter 游戏。我有一个完整的组件,但我需要在另一个组件中使用来自该组件的道具的值,以及在另一个组件中增加价值的函数。我该怎么做?

    class ClickCounter extends Component {   static mapStateToProps = (state) => {
        return {
          count: state.count
        };   };   static mapDispatchToProps = (dispatch) => {
        return bindActionCreators(
          {
            increaseCount,
            decreaseCount
          },
          dispatch
        );   };
         render() {
        const { increaseCount, decreaseCount } = this.props;
        return (
          <div>
          <button onClick={increaseCount}>+</button>
          <span>{this.props.count}</span>
          <button onClick={decreaseCount}>-</button>
          </div>
        );
}
}
 export default connect(
  ClickCounter.mapStateToProps,
  ClickCounter.mapDispatchToProps 
 )(ClickCounter);

标签: javascriptreactjsredux

解决方案


推荐阅读