【问题标题】:Redux Dataflow from Reducer to Container从 Reducer 到容器的 Redux 数据流
【发布时间】:2020-08-04 02:41:38
【问题描述】:

由于我是 redux 的新手,这里有一个关于 redux 中数据流的关键问题

据我了解,我创建了一个组件作为 CountN:

import React from 'react'

import styles from '../../features/counter/Counter.module.css'


const CountN = (props) => {
     
    const {countValue,actions} = props;
 
    
   

     return (
    
      <div>
           <div className={styles.row}>
                <button
                  className={styles.button}
                  aria-label="Increment value"
                  onClick={actions.increment}  
                >
                +
                </button>
                
                <span className={styles.value}>{ countValue }</span>
                
                <button
                  className={styles.button}
                  aria-label="Decrement value"
                  onClick={actions.decrement}
                >
                 -   
                </button>
           </div>
     </div>

          )

     }
        

export default CountN

然后我使用容器将数据传递给 CountN

下面的容器:

import React from 'react';
import CountN from "../../components/countN"

import { connect } from 'react-redux'
import * as CountActions from '../../actions'
import { bindActionCreators } from 'redux';

const mapStateToProps = (state) =>({
       countValue: state.value
})

const mapDispatchToProps = (dispatch) =>({
       actions: bindActionCreators(CountActions,dispatch)
})


export default connect(
     mapStateToProps,
     mapDispatchToProps
)(CountN)

为了管理状态,我创建了 Reducer 来设置状态:

下面的减速器:

import * as types  from '../constants/CountTypes';

const initialState = [{
    value: 0,
    payload: 0,

}]

 const counter = (state=initialState,action)=> {
    switch (action.type){
        case types.Increment:
            return [{
                value: state.value + 1,
                payload: 0,
            }]
        case types.Decrement:
            return [
                ...state,
                {
                    value: state.value - 1
                }
          
            ]
        case types.IncrementByAmount:
            return [{
                value: state.value + action.payload ,
                payload: action.payload
            }     
            ]
        default:
            return state
    
    }
};

export default counter;

另外,我用“CreateStore(reducer)”创建了一个商店来存储数据,

现在问题是我得到一个错误:

TypeError: 无法读取未定义的属性“增量”

据我所知,状态未定义,

能否请高手帮我弄清楚哪一部分是错的,为什么数据没有通过“props”传递给容器???

非常感谢

【问题讨论】:

    标签: redux react-redux dataflow


    【解决方案1】:

    您拥有的代码应该可以工作,但我确实对状态进行了一些更改,您将其定义为一个数组,但我没有看到为什么我将其更改为一个对象的原因。您的 mapStateToProps 不认为状态是一个数组,所以这可能是一个错误。请参阅下面我进行更改的代码中的 cmets。

    const { Provider, connect } = ReactRedux;
    const {
      createStore,
      applyMiddleware,
      compose,
      bindActionCreators,
    } = Redux;
    
    //I changed initialState to an object instead of an array
    const initialState = {
      value: 0,
      payload: 0,
    };
    //action types
    const types = {
      Increment: 'Increment',
      Decrement: 'Decrement',
      IncrementByAmount: 'IncrementByAmount',
    };
    //action creators
    const CountActions = {
      increment: () => ({ type: types.Increment }),
      decrement: () => ({ type: types.Decrement }),
    };
    const reducer = (state = initialState, action) => {
      switch (action.type) {
        case types.Increment:
          //changed to object
          return {
            value: state.value + 1,
            payload: 0,
          };
        case types.Decrement:
          //changed to object
          return {
            ...state,
            value: state.value - 1,
          };
        case types.IncrementByAmount:
          //changed to object
          return {
            value: state.value + action.payload,
            payload: action.payload,
          };
        default:
          return state;
      }
    };
    //creating store with redux dev tools
    const composeEnhancers =
      window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
    const store = createStore(
      reducer,
      initialState,
      composeEnhancers(
        applyMiddleware(() => (next) => (action) =>
          next(action)
        )
      )
    );
    const CountN = (props) => {
      const { countValue, actions } = props;
      return (
        <div>
          <div>
            <button
              aria-label="Increment value"
              onClick={actions.increment}
            >
              +
            </button>
            <span>{countValue}</span>
            <button
              aria-label="Decrement value"
              onClick={actions.decrement}
            >
              -
            </button>
          </div>
        </div>
      );
    };
    const mapStateToProps = (state) => ({
      countValue: state.value,
    });
    
    const mapDispatchToProps = (dispatch) => ({
      actions: bindActionCreators(CountActions, dispatch),
    });
    
    const App = connect(
      mapStateToProps,
      mapDispatchToProps
    )(CountN);
    
    ReactDOM.render(
      <Provider store={store}>
        <App />
      </Provider>,
      document.getElementById('root')
    );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.5/redux.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/7.2.0/react-redux.min.js"></script>
    <div id="root"></div>

    【讨论】:

    • 你好,HMR,非常感谢这个很好的解释,我按照你的修改做了,但代码仍然不起作用,它会产生同样的问题,也许是事实 我分开它们在不同的文件中导致问题?所以我找到了一个解决方案将组件 CountN 放入 Container 并且它工作得很好,仍然不知道为什么 当我将 CountN 与 Container 分开时无法工作但我有得到的印象是,当我们调用“连接”存储时,当代码分开时,状态不会被初始化。
    猜你喜欢
    • 2018-05-16
    • 2016-05-01
    • 2019-08-25
    • 2019-04-01
    • 2019-01-21
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    相关资源
    最近更新 更多