终极版/反应。实现后,combineReducers无法获得app的状态

时间:2018-04-02 15:55:43

标签: reactjs redux react-redux

将两个EditButton合并在一起(TodoAppTodoApp)后,我的应用每次都会崩溃。在它之前,当我只使用一个reducer reducers时,我对map没有任何问题。但现在我无法弄清楚出了什么问题,因为每次我都会在component以下的App函数中得到错误。错误" TypeError:无法读取属性' map'未定义"。

那么,我忘记了什么?此外,我无法在App的嵌套组件或容器中获取状态。它也很奇怪,但在import { combineReducers } from 'redux' import { ADD_TODO, EDIT_TODO, DELETE_TODO, FILTER_TODO_UP, FILTER_TODO_DOWN } from '../Variables/Variables' const initialState = { todos: [] } function EditButton(state, action) { if (typeof state === 'undefined') { return 'Edit'; } switch (action.type) { case EDIT_TODO: return state = "Edit" ? "Done" : "Edit" default: return state } } function TodoApp(state, action) { if (typeof state === 'undefined') { return initialState; } switch (action.type) { case ADD_TODO: return Object.assign({}, state, { todos: [ ...state.todos, { id: action.id, text: action.text, done: action.done } ] }); case EDIT_TODO: return Object.assign({}, state, { todos: [ ...state.todos, { id: action.id, text: action.text, done: action.done } ] }); case DELETE_TODO: return Object.assign({}, { todos: state.todos.filter(todos => todos.id !== parseInt(action.id)) }); case FILTER_TODO_UP: return Object.assign({}, { todos: [ ...state.todos.sort((a, b) => b.id - a.id) ] }); case FILTER_TODO_DOWN: return Object.assign({}, { todos: [ ...state.todos.sort((a, b) => a.id - b.id) ] }); default: return state; } } export default combineReducers({TodoApp, EditButton}) 我可以通过console.log()来做到这一点。

/ * REDUCERS * /

import React, { Fragment } from 'react';
import TodoFormAdd from '../Containers/TodoFormAdd';
import TodoListAdd from '../Containers/TodoListAdd';
import TodoFormFilterAdd from '../Containers/TodoFormFilterAdd';

class App extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return(
            <Fragment>
                // console.log(this.props.state.getState()) - work!
                <TodoFormAdd />
                <TodoListAdd store={this.props.store} />
                <TodoFormFilterAdd />
            </Fragment>
        );
    }
}

export default App;

/ * APP * /

import { connect } from 'react-redux';
import TodoList from '../Components/TodoList/TodoList';
import { DeleteTodo } from '../Actions/AddTodo'

// console.log(this.props.state.getState()) - does not work!

const mapStateToProps = state => ({
    todos: state.todos
});

const mapDispatchToProps = dispatch => ({
      todoFormDelete: todo => dispatch(DeleteTodo(todo))
});

export default connect(
    mapStateToProps, 
    mapDispatchToProps)(TodoList)

/ * CONTAINER * /

import React from 'react';
import TodoIteam from '../TodoIteam/TodoIteam'

class TodoList extends React.Component {
handleDelete = (e) => {
    let target = e.target;
    let closestDelete = target.closest('span');
    let closestEdit = target.closest('button');

    if (closestDelete) {
        let index = closestDelete.parentNode.getAttribute('index');
        this.props.todoFormDelete(index);
    } else { 
        return 
    }
}

render(props) {
// console.log(this.props.state.getState()) - does not work!

    return (
        <ul onClick={this.handleDelete}>{this.props.todos.map((iteam, index) => 
 // this where I get an error
                <TodoIteam key={index} index={iteam.id} {...iteam} />
            )}
        </ul>
    );
}
}

export default TodoList;

/ * COMPONENT * /

next

1 个答案:

答案 0 :(得分:3)

当您在combineReducers中使用ES6属性简写表示法时:

combineReducers({TodoApp, EditButton})

这相当于写combineReducers({ TodoApp: TodoApp, EditButton: EditButton })

但在你的CONTAINER内部,你正在访问state.todos,没有任何名为todos来自州而不是TodoApp,因此你的{{>>错误 1}}:

.map()

编辑: 当您从reducers中返回一个包含数组的对象this.props.todos.map((iteam, index) {} 时,为了访问正确的状态,您需要使用reducer Name,后跟要返回的数组名称todos

因此,在您的Container内,您需要访问正确的TodoApp.todos

reducer

您可以在Redux Documentation

上阅读有关combineReducers的更多信息
相关问题