代码之家  ›  专栏  ›  技术社区  ›  MachoBoy

如何使用redux子空间重用具有相同动作的reducer

  •  3
  • MachoBoy  · 技术社区  · 7 年前

    我正在使用React、语义ui React和redux子空间构建一个小应用程序。 我感谢任何能引导我走向正确方向的评论。

    PartA。js公司

    该组件呈现表格并用 <SubspaceProvider> .

    <Segment inverted color='black'>
    <h1>Age </h1>
    { this.state.toggle ?
    <SubspaceProvider mapState={state => state.withSpouseAge} namespace="withSpouseAge">
        <TableForm 
            headers={spouse_ageHeaders} 
            rows={spouse_ageData}  
            namespace={'withSpouseAge'}
        />
    </SubspaceProvider> :
    <SubspaceProvider mapState={state => state.withoutSpouseAge} namespace="withoutSpouseAge">
        <TableForm 
            headers={withoutSpouse_ageHeader} 
            rows={withoutSpouse_ageData}
            namespace={'withoutSpouseAge'}
        />
    </SubspaceProvider> }
    

    表格。js公司

    import React, { Component } from 'react';
    import { connect } from 'react-redux';
    import { Table } from 'semantic-ui-react';
    import { select } from '../actions';
    
    const shortid = require('shortid');
    
    class TableForm extends Component {
        constructor(props){
            super(props);
            this.state = {
                activeIndex: 0,
            }
            this.handleOnClick = this.handleOnClick.bind(this);
            this.isCellActive = this.isCellActive.bind(this);
        };
    
        isCellActive(index) {
            this.setState({ activeIndex: index });
        }
    
        handleOnClick(index, point) {
            this.isCellActive(index);
            this.props.onSelect(point);
        };
    
        tableForm = ({ headers, rows }) => {
            const customRenderRow = ({ factor, point, point2 }, index ) => ({
                key: shortid.generate(),
                cells: [
                    <Table.Cell content={factor || 'N/A'} />,
                    <Table.Cell 
                        content={point}
                        active={index === this.state.activeIndex}
                        textAlign={'center'} 
                        selectable 
                        onClick={() => this.handleOnClick(index, point)}
                    />,
                    <Table.Cell 
                        content={point2}
                        textAlign={'center'} 
                        selectable
                    />
                ],
            });
            return (
                <Table
                    size='large'
                    padded
                    striped
                    celled
                    verticalAlign={'middle'} 
                    headerRow={this.props.headers} 
                    renderBodyRow={customRenderRow} 
                    tableData={this.props.rows}
                />
            )
        };
    
        render() {
            console.log(this.props.withSpouseAgePoint);
            const { headers, rows } = this.props;
            return (
                <div>
                    {this.tableForm(headers, rows)}
                </div>
            );  
        }
    };
    
    const mapDispatchToProps = (dispatch) => {
        return {
            onSelect: (point) => {dispatch(select(point))},
        }
    }
    
    const mapStateToProps = state => {
        return {
            withSpouseAgePoint: state.withSpouseAge,
            withSpouseLoePoint: state.withSpouseLoe,
        }
    }
    
    export default connect(mapStateToProps, mapDispatchToProps)(TableForm);
    

    行动

    import {
        SELECT,
    } from './types';
    
    export const select = (points) => ({
        type: 'SELECT',
        points,
    });
    

    减速器。js公司

    import { SELECT } from '../actions/types';
    
    const INITIAL_STATE = {
        point: 0,
    };
    
    const selectionReducer = (state = INITIAL_STATE, action) => {
        switch (action.type) {
            case 'SELECT':
                return { ...state, point: state.point + action.points };
            default:
                return state;
        }
    };
    
    export default selectionReducer;
    

    import { createStore, combineReducers } from 'redux';
    import { subspace, namespaced } from 'redux-subspace';
    import selectionReducer from './selectionReducer';
    import toggleReducer from './toggleReducer';
    
    const reducers = combineReducers({
        withSpouseAge: namespaced('withSpouseAge')(selectionReducer),
        withSpouseLoe: namespaced('withSpouseLoe')(selectionReducer),
        withSpouseOlp: namespaced('withSpouseOlp')(selectionReducer),
        withSpouseOlp2: namespaced('withSpouseOlp2')(selectionReducer),
        withSpouseExp: namespaced('withSpouseExp')(selectionReducer),    
        withoutSpouseAge: namespaced('withoutSpouseAge')(selectionReducer),
        withoutSpouseLoe: namespaced('withoutSpouseLoe')(selectionReducer),
        withoutSpouseOlp: namespaced('withoutSpouseOlp')(selectionReducer),
        withoutSpouseOlp2: namespaced('withoutSpouseOlp2')(selectionReducer),
        withoutSpouseExp: namespaced('withoutSpouseExp')(selectionReducer),
        toggle: toggleReducer,
    });
    

    使现代化

    我在下面添加了表格组件

    const mapDispatchToProps = (dispatch) => {
        return {
            onSelect: (point) => {dispatch(select(point))},
        }
    }
    
    const mapStateToProps = state => {
        return {
            withSpouseAgePoint: state.withSpouseAge,
            withSpouseLoePoint: state.withSpouseLoe,
        }
    }
    
    export default connect(mapStateToProps, mapDispatchToProps)(TableForm);
    

    使生效 this.props.onSelect(point) 点击。它仍然显示了相同的结果 undefined getState() . consloe.log . 我认为我对redux子空间的实现是错误的。我上传了整个TableForm组件,还更新了reducer。请帮帮我!

    更新2

    我替换了 mapStateToProps 但还有一个问题,当我点击单元格时,所有状态的值都相同。 console.log(props) . 我的计划是每个州都有自己的价值存储。

    const mapStateToProps = state => {
        return {
            withSpouseAgePoint: state,
            withoutSpouseAge: state,
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   JustinTRoss    7 年前

    您已经将您的组件命名为with spousage,并将状态映射到状态。在您的子空间提供者中使用Spuseage。因此,你称之为状态的等价物。与斯普希奇。WITHSPUSAGE(未定义)。

    http://www.sohamkamani.com/blog/2017/03/31/react-redux-connect-explained/#connect :

    import {connect} from 'react-redux'
    
    const TodoItem = ({todo, destroyTodo}) => {
      return (
        <div>
          {todo.text}
          <span onClick={destroyTodo}> x </span>
        </div>
      )
    }
    
    const mapStateToProps = state => {
      return {
        todo : state.todos[0]
      }
    }
    
    const mapDispatchToProps = dispatch => {
      return {
        destroyTodo : () => dispatch({
          type : 'DESTROY_TODO'
        })
      }
    }
    
    export default connect(
      mapStateToProps,
      mapDispatchToProps
    )(TodoItem)