react-redux Reducer 不会将对象传递给商店

酷派

我有一个问题,我真的可以使用一些帮助。我对 react 和 react-redux 很陌生,所以解决方案可能比我想象的要简单(我希望如此)。我有一个简单的站点,其中包含带有食谱名称的链接(来自 react-router):

class Posts extends Component {

    componentWillMount() {
        this.props.fetchPosts();
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.newPost) {
            this.props.posts.unshift(nextProps.newPost);
        }
    }


    render() {
        const postItems = this.props.posts.map(recipe => (
            <div key={recipe.id}>
                <Link to={`/recipe/id/${recipe.id}`}>{recipe.name}</Link>
            </div>
        ));

        return (

            <Router>
            <div>
                <h1>Well yeah</h1>
                {postItems}
                <Route path={`/recipe/id/:recipeId`} component={Post}/>
            </div>
            </Router>
        );
    }
}
Posts.propTypes = {
    fetchPosts: PropTypes.func.isRequired,
    posts: PropTypes.array.isRequired,
    newPost: PropTypes.object,
};

const mapStateToProps = state => ({
    posts: state.posts.items,
    newPost: state.posts.item,
});

export default connect(mapStateToProps, {fetchPosts})(Posts);

现在,我还有一个组件 Post,它应该是一个单一的配方。

class Post extends Component {
    static propTypes = {};

    componentDidMount() {
        this.props.fetch();
    }

    render() {
        return (
            <div>Hello{this.props.recipe.name}</div>
        );
    }
}

Post.propTypes = {
    fetch: PropTypes.func.isRequired,
    recipe: PropTypes.object
};

const mapDispatchToProps = (dispatch, ownProps) => ({
    fetch: () => {
        dispatch(fetchPostById(ownProps.location.pathname))
    }
});
const mapStateToProps = state => ({

    recipe: state.posts.recipe
});
export default connect(mapStateToProps, mapDispatchToProps)(Post);

无论我做什么,我似乎都无法使其正常工作。我有一个获取单个配方的操作(获取配方列表没有问题),然后将其传递给减速器。但是,当我将 console.log(action.payload) 放在 reducer 中的案例中时,它在控制台中显示包含配方的对象就好了。但是当我把 {this.props.recipe.name} 放在 Post.js 中时,我得到一个错误 this.props.recipe 是未定义的。非常感谢任何帮助。可以肯定的是,我也会发布减速器的代码和操作。

减速器:

const initialState = {
    items: [],
    item: {},
    recipe:{}

};

export default function (state = initialState, action) {
    switch (action.type) {
        case FETCH_RECIPES:
            return {
                ...state,
                items: action.payload
            };
        case NEW_RECIPE:
            return {
                ...state,
                item: action.payload
            };
        case FETCH_RECIPE_ID:
            return{
                ...state,
                recipe:action.payload
            };
        default:
            return state;
    }

}

行动:

export const fetchPosts = () => dispatch => {
    fetch('http://192.168.1.3:6996/recipe')
        .then(res => res.json())
        .then(recipes => dispatch({
            type: FETCH_RECIPES,
            payload: recipes
        }));

};
export const createPost = (postData) => dispatch => {
    fetch('http://192.168.1.3:6996/recipe', {
        method: 'POST',
        headers: {
            'content-type': 'application/json'
        },
        body: JSON.stringify(postData)
    }).then(res => res.json()).then(recipe => dispatch({
        type: NEW_RECIPE,
        payload: recipe
    }))

};
export const fetchPostById= (id) => dispatch => {
    fetch('http://192.168.1.3:6996' + id)
        .then(recipe => recipe.json())
        .then(recipe => dispatch({
        type: FETCH_RECIPE_ID,
        payload: recipe
    }))
};

主减速机:

import { combineReducers } from 'redux'
import postReducer from './postReducer'

export default combineReducers({
    posts: postReducer
});
酷派

在晚上关闭 WebStorm 后,该站点在第二天早上启动并运行得很好。我的反应路由器部分有问题,但我会发布不同的问题,所以我不会违反任何规则。非常感谢!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将对象传递给react-redux

来自分类Dev

React-Redux Reducer 不会被调用

来自分类Dev

React + Redux - Reducer 子状态不是对象?

来自分类Dev

在 React+Redux 中将参数传递给 reducer 的正确方法?

来自分类Dev

React/Redux 将输入值传递给 reducer.js

来自分类Dev

动作不会在React + Redux中触发reducer

来自分类Dev

在react-redux 中的reducer 之后不会触发`mapStateToProps`

来自分类Dev

在Reducer中进行React / Redux,链式或Promise

来自分类Dev

理解 React Redux Reducer 和 MapstateToProps

来自分类Dev

将 redux 状态传递给 reducer

来自分类Dev

我如何从React-Redux中的reducer状态更新对象数组中单个元素的名称字段?

来自分类Dev

如何使用 react-redux-forms 和自定义 reducer 避免表单对象的奇怪嵌套

来自分类常见问题

在Redux Reducer中读取商店的初始状态

来自分类Dev

Redux Reducer不会将更新后的状态返回到UI

来自分类Dev

在Redux Reducer中导出默认对象

来自分类Dev

Redux Reducer:筛选出重复的对象?

来自分类Dev

Redux Reducer不更新状态对象

来自分类Dev

重用Redux-react异步请求动作创建器/ Reducer

来自分类Dev

React:减少动作导致Redux Reducer中的状态不确定

来自分类Dev

无法从React库npm包(redux)导入reducer

来自分类Dev

Reducer在Redux React JS上不起作用

来自分类Dev

React Native Redux Reducer 与 Immutability Helper 推送数组的正确方法

来自分类Dev

React-Redux 初始化来自 FIeldArray 的 reducer 的值

来自分类Dev

React-Redux和Material UI:组件不会将存储传递给“已连接”的子级

来自分类Dev

尽管react-redux中的reducer状态,但ACTION并未附加到reducer

来自分类Dev

Redux商店没有有效的reducer

来自分类Dev

商店初始化后更新redux reducer

来自分类Dev

Redux Reducer Promise处理

来自分类Dev

Redux创建Reducer预期

Related 相关文章

  1. 1

    如何将对象传递给react-redux

  2. 2

    React-Redux Reducer 不会被调用

  3. 3

    React + Redux - Reducer 子状态不是对象?

  4. 4

    在 React+Redux 中将参数传递给 reducer 的正确方法?

  5. 5

    React/Redux 将输入值传递给 reducer.js

  6. 6

    动作不会在React + Redux中触发reducer

  7. 7

    在react-redux 中的reducer 之后不会触发`mapStateToProps`

  8. 8

    在Reducer中进行React / Redux,链式或Promise

  9. 9

    理解 React Redux Reducer 和 MapstateToProps

  10. 10

    将 redux 状态传递给 reducer

  11. 11

    我如何从React-Redux中的reducer状态更新对象数组中单个元素的名称字段?

  12. 12

    如何使用 react-redux-forms 和自定义 reducer 避免表单对象的奇怪嵌套

  13. 13

    在Redux Reducer中读取商店的初始状态

  14. 14

    Redux Reducer不会将更新后的状态返回到UI

  15. 15

    在Redux Reducer中导出默认对象

  16. 16

    Redux Reducer:筛选出重复的对象?

  17. 17

    Redux Reducer不更新状态对象

  18. 18

    重用Redux-react异步请求动作创建器/ Reducer

  19. 19

    React:减少动作导致Redux Reducer中的状态不确定

  20. 20

    无法从React库npm包(redux)导入reducer

  21. 21

    Reducer在Redux React JS上不起作用

  22. 22

    React Native Redux Reducer 与 Immutability Helper 推送数组的正确方法

  23. 23

    React-Redux 初始化来自 FIeldArray 的 reducer 的值

  24. 24

    React-Redux和Material UI:组件不会将存储传递给“已连接”的子级

  25. 25

    尽管react-redux中的reducer状态,但ACTION并未附加到reducer

  26. 26

    Redux商店没有有效的reducer

  27. 27

    商店初始化后更新redux reducer

  28. 28

    Redux Reducer Promise处理

  29. 29

    Redux创建Reducer预期

热门标签

归档