Reactjs Can';t地图道具传递到我的React路由器';s链接组件

Reactjs Can';t地图道具传递到我的React路由器';s链接组件,reactjs,react-router,react-router-v4,Reactjs,React Router,React Router V4,我从父母那里传递道具,如下所示: {Object.keys(this.state.recipes).map(key => { return ( <Link key={key} to={{ pathname: `/view/${key}`, state: { recipe: this.state.recipes[key] } }} > <R

我从父母那里传递道具,如下所示:

{Object.keys(this.state.recipes).map(key => {
  return (
    <Link
      key={key}
      to={{
        pathname: `/view/${key}`,
        state: {
          recipe: this.state.recipes[key]
        }
      }}
    >
      <Recipe
        key={key}
        index={key}
        recipe={this.state.recipes[key]}
        deleteRecipe={this.deleteRecipe}
        editRecipe={this.editRecipe}
      />
    </Link>
  );
})}
{Object.keys(this.state.recipes.map)(key=>{
返回(
);
})}
这就是我的路由器的外观:

<BrowserRouter>
  <div>
    <Route exact path="/" component={App} />
    <Route path="/view/:recipeId" component={ViewRecipe} />
  </div>
</BrowserRouter>

从这个组件中,我试图访问这些道具

ViewRecipe.js

class ViewRecipe extends React.Component {
  constructor(props) {
    super(props);
    this.state = { recipe: {} }
  }

  componentDidMount() {
    const { recipe } = this.props.location.state;
    this.setState({ recipe: recipe });
  }

  render() {
    return (
      <div>
       <h1>{this.state.recipe.recipeName}</h1>
    <img src={this.state.recipe.dishImg} alt="Dish" />
        <ul>
           {this.state.recipe.ingredients.map( ingredient => <li>{ingredient}</li>)}
        </ul>
        <ul>
          <li>direction</li>
          <li>direction</li>
          <li>direction</li>
          <li>direction</li>
          <li>directio</li>
        </ul>
      </div>
    );
  }
}
类ViewRecipe扩展了React.Component{
建造师(道具){
超级(道具);
this.state={recipe:{}
}
componentDidMount(){
const{recipe}=this.props.location.state;
this.setState({recipe:recipe});
}
render(){
返回(
{this.state.recipe.recipeName}
    {this.state.recipe.components.map(配料=>
  • {component}
  • )}
  • 方向
  • 方向
  • 方向
  • 方向
  • 导演
); } }
我的ViewRecipe.js状态如下所示:


recipeName和dishImg渲染正确,但当我尝试映射我的成分时,出现以下错误

这可能是未定义的

    <ul>
       {this.state.recipe.ingredients.map( ingredient => <li>{ingredient}</li>)}
    </ul>
    {this.state.recipe.components.map(配料=>
  • {component}
  • )}
为此,您必须修改这样的代码才能工作

    <ul>
       {this.state.recipe && this.state.recipe.ingredients && this.state.recipe.ingredients.map( ingredient => <li>{ingredient}</li>)}
    </ul>
    {this.state.recipe&&this.state.recipe.components&&this.state.recipe.components.map(component=>
  • {component}
  • )}
上面我们正在检查的是所有在做map之前定义的内容,希望这能起作用

编辑:


不能对非数组使用.map函数。null.map results to undefined:)

这可能是未定义的

    <ul>
       {this.state.recipe.ingredients.map( ingredient => <li>{ingredient}</li>)}
    </ul>
    {this.state.recipe.components.map(配料=>
  • {component}
  • )}
为此,您必须修改这样的代码才能工作

    <ul>
       {this.state.recipe && this.state.recipe.ingredients && this.state.recipe.ingredients.map( ingredient => <li>{ingredient}</li>)}
    </ul>
    {this.state.recipe&&this.state.recipe.components&&this.state.recipe.components.map(component=>
  • {component}
  • )}
上面我们正在检查的是所有在做map之前定义的内容,希望这能起作用

编辑:


不能对非数组使用.map函数。null.map results to undefined:)

componentDidMount中配方的值是多少?发生此错误的可能性最大,因为在
渲染时配料数组为null或未定义“位置”:{“路径名”:“/查看/接收”、“搜索”:““哈希”:““状态”:{“配方”:{“recipeName”:“火腿和奶酪早餐袋”、“配料”:[“Prod 1”、“Prod 2”、“Prod 3”],“方向”:[“第一方向”、“第二方向”、“第三方向”],“dishImg”:“}”,键“:“ripdex”}”,componentDidMount
中配方的值是多少?发生此错误的可能性最大,因为在
呈现
时配料数组为空或未定义“位置”:{“路径名”:“/view/recipe1”,“搜索”:“,“哈希”:“,“状态”:{“配方”:{“recipeName”:”火腿和奶酪早餐袋、“配料”:[“产品1”、“产品2”、“产品3”],“方向”:[“第一方向”、“第二方向”、“第三方向”],“dishImg”:“}}”,键“:“ripdex”}”,如何
this.state.recipe&&this.state.recipe.incoments.length?this.state.recipe.incoments.map(…):null
?因此,除非
配料
数组中有一些数据,否则我们将阻止执行
映射
。Yaa我们可以更改this.state.recipe&&this.state.recipe.components.length&&this.state.recipe.components.map(…)像这样,但我认为这里不需要三元运算符,因为它只有在数组有一些值(this.state.recipe.incoments.length)时才会运行,
this.state.recipe&&this.state.recipe.incoments.length?this.state.recipe.incoments.map(…):null
?因此,除非
配料
数组中有一些数据,否则我们将阻止执行
映射
。Yaa我们可以更改this.state.recipe&&this.state.recipe.components.length&&this.state.recipe.components.map(…)像这样,但我认为这里不需要三元运算符,因为在数组有一些值(this.state.recipe.components.length)之前,它不会运行