【问题标题】:Redux combineReducers Uncaught Type error: Cannot convert undefined or null to objectRedux combineReducers 未捕获类型错误:无法将 undefined 或 null 转换为对象
【发布时间】:2018-04-20 01:26:36
【问题描述】:

我开始反应和减少,我已经使用 combineReducers 并得到 ​​p>

未捕获的类型错误:无法将未定义或 null 转换为对象。

没有 combineReducer 它工作正常。下面是我的 Reducer 的 sn-p。

reducers\article.js:

import { ADD_ARTICLE } from "../actiontypes/action-types";

const initialState = {
articles: []
};

const articleReducer = (state = initialState, action) => {

switch (action.type){
case ADD_ARTICLE:
return {...state, articles: [...state.articles, action.payload] };
default:
return state;
}
};

export default articleReducer;

reducers\index.js

import { combineReducers } from "redux";
import articleReducer from "./article";

export default combineReducers({
articles : articleReducer
});

actions\index.js

import {ADD_ARTICLE} from "../actiontypes/action-types";

export const addArticle = article => ({type: ADD_ARTICLE, payload: article});

store/index.js

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';

const initialArticlesState = {
    articles: [{"title":"some title", "id":"04503"}]
};

const middleware = [thunk];
const store = createStore(
    rootReducer,
    initialArticlesState,
    compose(
        applyMiddleware(...middleware),
        window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()  
    )

);

export default store;

components\form.js

import React, { Component } from "react";
import { connect } from "react-redux";
import uuidv1 from "uuid";
import { addArticle } from "../js/actions/index";

class ConnectedForm extends Component {
    constructor(){
        super();
        this.state = {
            title: ""
        };

        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleSubmit(event) {

        event.preventDefault();
        const { title } = this.state;
        const id = uuidv1();
        console.log("submit",{ title, id });
        this.props.addArticle();
        this.setState({ title: "" });
    }

    handleChange(event) {
        this.setState({ [event.target.id]: event.target.value});
    }

    render() {
        const { title } = this.state;
        return (
            <form onSubmit={this.handleSubmit}>
                <div className="form-group">
                <label htmlFor="title">Title</label>
                <input
                    type="text"
                    className="form-control"
                    id="title"
                    value={title}
                    onChange={this.handleChange}
                />
                </div>
                <button type="submit" className="btn btn-success btn-lg">
                SAVE
                </button>
            </form>
        );
    }
}

const mapDispatchToProps = dispatch => {
    return {
        addArticle: article => dispatch(addArticle(article))
    };
};

const Form = connect(null, mapDispatchToProps)(ConnectedForm);

export default Form;

【问题讨论】:

  • 您有一些奇怪的字符,例如 或三个点。有没有副本/过去的问题?
  • 那些是三个点 (...) 扩展运算符,我认为在将其粘贴到此处时它已更改。我已经编辑了帖子。
  • 我知道他们是传播运营商,但不是标准运营商 :) 创建商店对我来说没有问题:codesandbox.io/s/5zk94ylnxn 当然我没有尝试过任何操作,但商店方面还可以。你是如何行动的,你如何应用它?如果可能,请发布您的整个代码。
  • 能否请您在创建商店的位置发布代码?
  • 更新帖子

标签: reactjs redux


【解决方案1】:

首先像这样改变你的initialArticleState:

const initialArticlesState = [
    { title: "some title", id: "04503" },
];

您可能希望将状态作为对象数组。那些对象是文章吧?因此,当您设置初始状态时,由于 combineReducers,您正在公开文章状态。您的顶级 redux 状态现在包括一个包含文章对象的文章数组。

然后,像这样更改您的减速器返回语句:

return [ ...state, action.payload ];

现在,如果您想要文章状态中的文章以外的其他内容,当然可以更改状态形状。例如:

const initialArticlesState = {
    articles: [
        { title: "some title", id: "04503" },
    ],
    totalArticleNumber: 1,
};

如果您这样做,那么您将拥有一个文章状态,其中包括文章和总文章编号。在这种情况下,您可能想更改状态名称。

Reducer 是这样的:

return { ...state, articles: [ ...state.articles, action.payload ] };

【讨论】:

  • 在对状态形状进行上述修改后使其工作,但收到警告消息:reducer 收到的先前状态具有意外类型的“数组”。预期参数是具有以下键的对象:“articles”。
  • 你是如何改变的?我现在创建了另一个代码框,运行良好:codesandbox.io/s/v8vwm21vol 我已经稍微清理了你的代码。
  • 是的,它现在工作正常,我在另一个组件中将不正确的参数传递给 mapStateToProps。感谢您的帮助。
猜你喜欢
  • 2021-09-01
  • 1970-01-01
  • 2022-12-17
  • 2020-03-28
  • 1970-01-01
  • 2020-08-15
  • 2020-08-02
  • 2018-11-21
  • 2021-12-17
相关资源
最近更新 更多