如何将减速器的初始状态传递给组件?

时间:2018-12-10 23:58:39

标签: javascript reactjs ecmascript-6 redux

我有这个动作

cancellations.js

const toggleCheckboxAction = id => ({
  type: ActionTypes.TOGGLE_CHECKBOX,
  payload: id,
});

这个减速器:

const initialState = {
  checkboxes: [
    {
      labelText: '...'
      checked: true,
      value: 'itemsCancelled',
      id: 'checkBoxItemsCancelled',
    },
    {
      labelText: '...'
      checked: true,
      value: 'requestDate',
      id: 'checkboxRequestDate',
    },
    {
      labelText: '...'
      checked: true,
      value: 'status',
      id: 'checkboxStatus',
    },
    {
      labelText: '...'
      checked: true,
      value: 'requestedBy',
      id: 'checkboxRequestedBy',
    },
  ],
}

[ActionTypes.TOGGLE_CHECKBOX](state = initialState.checkboxes, action = {}) {
    return state.map(checkbox => {
      if (checkbox.id !== action.payload.id) {
        return checkbox;
      }
      return {
        ...checkbox,
        checked: !checkbox.checked,
      };
    });
  },

我有这个组件,需要在其中使用该动作/减速器。

import React from 'react';
import connect from 'react-redux/es/connect/connect';
import { Checkbox } from 'carbon-components-react';
import { compose } from 'redux';
import PropTypes from 'prop-types';
import { toggleCheckboxAction } from '../actions/cancellations';

    const CheckboxList = ({ checkboxes, dispatch }) =>
      // This checkboxes array is what I need to get from the reducer
      checkboxes.map(checkbox => (
        <Checkbox
          key={checkbox.id}
          id={checkbox.id}
          labelText={checkbox.labelText}
          value={checkbox.value}
          checked={checkbox.checked}
          onChange={() => {
            dispatch(toggleCheckboxAction(checkbox.id));
          }}
        />
      ));

    CheckboxList.propTypes = {
      toggleCheckboxesHandler: PropTypes.func.isRequired,
    };

    export default compose(
      connect(
        // I guess here I have to do the connection logic
      ),
    )(CheckboxList);

现在我收到此错误:

  

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

来自上面组件中的checkboxes.map(checkbox => (...)

您有想法如何使用来自reducer的数组初始化我的组件吗?

我不知道我是否可以使用类似mapStateToProps的东西

2 个答案:

答案 0 :(得分:0)

您似乎尚未设置mapStateToPropsmapDispatchToProps

Here are the relevant docs.

您想做这样的事情:(长格式):

const mapStateToProps = (state, ownProps)=>( {
     checkboxes : state[ActionTypes.TOGGLE_CHECKBOX]
}) ;

export default compose(
  connect(
    mapStateToProps
  ),
)(CheckboxList);

答案 1 :(得分:0)

您缺少一些必要的逻辑来完成此工作。您需要在代码末尾包含mapToStateProps并将其连接。

如何使用mapToStateProps

之后:

CheckboxList.propTypes = {
      toggleCheckboxesHandler: PropTypes.func.isRequired,
    };

放置此:

const mapStateToProps = state => ({
  //whatever you are bringing over
  //example auth: state.auth 
});

并像这样连接它:

export default connect(
  mapStateToProps,
  { toggleCheckboxesHandler }
)(CheckboxList);

一起,在渲染方法之后,您的代码应如下所示:

CheckboxList.propTypes = {
          toggleCheckboxesHandler: PropTypes.func.isRequired,
        };
const mapStateToProps = state => ({
      //whatever you are bringing over
      //example auth: state.auth 
 });
 export default connect(
      mapStateToProps,
      { toggleCheckboxesHandler }
    )(CheckboxList);

希望这会有所帮助。