简体   繁体   English

我的 mapStateToProps 中的 state 出现 undefined

[英]My state in my mapStateToProps comes up undefined

I had an issue following other questions concerning this so here goes nothing.我在其他有关此问题的问题之后遇到了问题,所以这里什么都没有。

So I am using redux thunk to 'post' to a database and hopefully display on the frontend view using fetch....but that's not where my issue is yet and just want you to understand where I will want to end up at some point.所以我正在使用 redux thunk 来“发布”到数据库,并希望使用 fetch 显示在前端视图中......但这还不是我的问题所在,只是想让你了解我在某个时候想要结束的地方. So my issue is I am getting a 'TypeError: state is undefined' on my mapStateToProps function and I thought I was setting this up right.所以我的问题是我的 mapStateToProps function 上出现“TypeError:state 未定义”,我认为我设置正确。

Below is the code snippet that shows up in my browser showing where the problem is.下面是显示在我的浏览器中的代码片段,显示了问题所在。

0 | const mapStateToProps = state =>{
  31 |     return{
> 32 |         recipes: state.recipes,
  33 |     }
  34 | }
  35 | 

.....I hope I don't lose anyone on this next part .....我希望我不会在下一部分失去任何人

So far....here is my RecipeContainer component where my mapStateToProp function is defined and connected.到目前为止....这是我的 RecipeContainer 组件,其中定义并连接了我的 mapStateToProp function。

import React, { Component } from 'react'
import RecipeList from '../components/RecipeList'
import RecipeInput from '../components/RecipeInput'
import { connect } from 'react-redux'
import { postRecipes } from '../actions/fetchRecipes.js'

class RecipeContainer extends Component{
    constructor(props){
        super(props)
    }


    componentDidMount(){
        this.props.postRecipes()
    }

    render(){
        return (
            <div>
               <RecipeInput addRecipe={this.props.addRecipe}/> 
               <RecipeList recipes={this.props.recipes} deleteRecipe={this.props.deleteRecipe}/>
            </div>
        )
    }

}



const mapStateToProps = state =>{
    return{
        recipes: state.recipes,
    }
}


const mapDispatchToProps= dispatch =>{
    return{
    addRecipe: () => dispatch(postRecipes()),
    deleteRecipe: id => dispatch({type: 'Delete_Recipe', id})
    }
}


export default connect(mapStateToProps,mapDispatchToProps)(RecipeContainer)

Here is my reducer showing where I put together the 'Add_Recipe' action type这是我的减速器,显示了我将“Add_Recipe”操作类型放在一起的位置

import cuid from 'cuid';
export const cuidFn = cuid

export default function manageRecipes(state={
    recipes:[],
    catagories:[],
}, action){


    switch(action.type){
        case 'Add_Recipe':
            const recipe = {
                name: action.name,
                ingredients: action.ingredients,
                chef_name: action.chef_name,
                origin: action.origin,
                catagoryId: action.catagoryId,
                catagories: action.catagories,
                id: cuidFn()
            }

            return{
                ...state,
                recipes: [...state.recipes, recipe],
            }

}

And lastly(not sure how relevant this is).....here is my 'postRecipe' action defined in it's own file最后(不确定这有多相关).....这是我在它自己的文件中定义的“postRecipe”操作

export function postRecipes(){
    const BASE_URL = `http://localhost:10524`
    const RECIPES_URL =`${BASE_URL}/recipes`
    const config = {
        method: "POST",
        body:JSON.stringify(recipe),
        headers: {
        "Accept": "application/json",
        "Content-type": "application/json"
     }
    }
    return(dispatch)=>{
    
    fetch(RECIPES_URL,config)
    .then(response => 
    response.json())
    .then(resp => {
        dispatch({
            type: 'Add_Recipe',
            paylaod:{
                name: resp.name,
                ingredients: resp.ingredients,
                chef_name: resp.chef_name,
                origin: resp.origin,
                catagoryId: resp.catagoryId 
            }
        })
    })
      .catch((error) => console.log.error(error))

    }
    
    
}

I want to know basically how my state as initially mentioned in my mapStateToProp function is coming up undefined, and if I missed anything in the code I presented.我想知道我的 state 最初在我的 mapStateToProp function 中是如何出现未定义的,如果我错过了我提供的代码中的任何内容。 Redux has been a little tough to grasp. Redux 有点难以掌握。

It should work if you change paylaod to payload.如果您将paylaod更改为有效负载,它应该可以工作。 You've got undefined in state, since the payload is not sent with dispatch.您在 state 中未定义,因为有效负载不是随调度发送的。

 export function postRecipes() { const BASE_URL = `http://localhost:10524` const RECIPES_URL = `${BASE_URL}/recipes` const config = { method: "POST", body: JSON.stringify(recipe), headers: { "Accept": "application/json", "Content-type": "application/json" } } return (dispatch) => { fetch(RECIPES_URL, config).then(response => response.json()).then(resp => { dispatch({ type: 'Add_Recipe', payload: { name: resp.name, ingredients: resp.ingredients, chef_name: resp.chef_name, origin: resp.origin, catagoryId: resp.catagoryId } }) }).catch((error) => console.log.error(error)) } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM