Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Redux进行反应,组件无法识别从父级传递的属性_Javascript_Reactjs_Redux_React Redux - Fatal编程技术网

Javascript 使用Redux进行反应,组件无法识别从父级传递的属性

Javascript 使用Redux进行反应,组件无法识别从父级传递的属性,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,请仔细阅读下面的代码,如果不够清楚,这里有一个扩展版本的我的问题 因此,我将一些属性从父组件传递给子组件。父组件是我的组件,它被包装在组件中。允许它访问存储。现在,在child中,我有一个函数,它根据从父级传递的道具呈现信息,这是我的child comp。代码 //主组件 ... const RenderCard=({item,isLoading,errMess})=>{ console.log(item.name) 如果(孤岛加载){ 返回( ) } 否则如果(错误){ 返回( {errM

请仔细阅读下面的代码,如果不够清楚,这里有一个扩展版本的我的问题

因此,我将一些属性从父组件传递给子组件。父组件是我的
组件,它被包装在
组件中。允许它访问存储。现在,在child中,我有一个函数,它根据从父级传递的道具呈现信息,这是我的child comp。代码

//主组件
...
const RenderCard=({item,isLoading,errMess})=>{
console.log(item.name)
如果(孤岛加载){
返回(
)
} 
否则如果(错误){
返回(
{errMess}
)
}
否则{
返回(
{item.name}
) 
}
}
const Home=props=>{
返回(
)
}
...

问题是,在
Main
组件的渲染方法中没有返回任何组件。您已经声明了一个方法
HomePage

请参阅下面的代码。这应该行得通

更新:

下面是一个有效的沙箱:

你在盘子上菜前试着渲染。因此,增加了一项条件检查,以确定盘子是否已装入

function Home(props) {
  return (
    !props.dishesLoading && (
      <div className="container">
        <div className="row align-items-start">
          <div className="col-12 col-md m-1">
            <RenderCard
              item={props.dish}
              isLoading={props.dishesLoading}
              errMess={props.dishesErrMess}
            />
          </div>
        </div>
      </div>
    )
  );
}
功能主页(道具){
返回(
!props.dishesLoading&&(
)
);
}

问题是,在
Main
组件的渲染方法中没有返回任何组件。您已经声明了一个方法
HomePage

请参阅下面的代码。这应该行得通

更新:

下面是一个有效的沙箱:

你在盘子上菜前试着渲染。因此,增加了一项条件检查,以确定盘子是否已装入

function Home(props) {
  return (
    !props.dishesLoading && (
      <div className="container">
        <div className="row align-items-start">
          <div className="col-12 col-md m-1">
            <RenderCard
              item={props.dish}
              isLoading={props.dishesLoading}
              errMess={props.dishesErrMess}
            />
          </div>
        </div>
      </div>
    )
  );
}
功能主页(道具){
返回(
!props.dishesLoading&&(
)
);
}

问题在于,在主组件中,您将dishesLoading道具错误地传递给主组件。您需要从名为
isLoading
而不是
dishesLoading

const HomePage = () => {
      return (
        <Home
          dish={this.props.dishes.dishes.filter(dish => dish.featured)[0]}
          dishesLoading={this.props.dishes.isLoading}
          dishesErrMess={this.props.dishes.errMess}
        />
      );
};
const主页=()=>{
返回(
dish.featured)[0]}
dishesLoading={this.props.disks.isLoading}
dishesErrMess={this.props.disks.errMess}
/>
);
};


另外,在
RenderCard
组件中,在加载数据之前,您不应该访问
item.name
,因为在
console.log(item.name)

中,最初它的
未定义。您需要从名为
isLoading
而不是
dishesLoading

const HomePage = () => {
      return (
        <Home
          dish={this.props.dishes.dishes.filter(dish => dish.featured)[0]}
          dishesLoading={this.props.dishes.isLoading}
          dishesErrMess={this.props.dishes.errMess}
        />
      );
};
const主页=()=>{
返回(
dish.featured)[0]}
dishesLoading={this.props.disks.isLoading}
dishesErrMess={this.props.disks.errMess}
/>
);
};


另外,在
RenderCard
组件中,在加载数据之前,您不应该访问
item.name
,因为它最初在
console.log(item.name)

中的
未定义的
关于样式:您不需要
return
语句,如果您只返回一些内容,它对阅读没有帮助。使用
道具类型
库,并在第一次得到错误类型的
道具的地方找到它
。使用像jest这样的单元测试-它非常容易使用,而且你不会在这些问题上浪费时间。如果你不想使用
道具类型
库,至少使用js文档来获得更好的IDE支持。谢谢你的评论,但我只是在基于父级传递的道具有条件地呈现某些东西时才使用return。关于JEST测试,嗯,我需要现在解决这个问题,然后我会考虑看单元测试的风格:你不需要<代码>返回< /Cord>语句之后,如果你只返回一些东西,它不利于阅读。使用
道具类型
库,并在第一次得到错误类型的
道具的地方找到它
。使用像jest这样的单元测试-它非常容易使用,而且你不会在这些问题上浪费时间。如果你不想使用
道具类型
库,至少使用js文档来获得更好的IDE支持。谢谢你的评论,但我只是在基于父级传递的道具有条件地呈现某些东西时才使用return。关于JEST测试,嗯,我需要现在解决这个问题,然后我会考虑查看单元测试,请您重新检查代码框,我最初忘记添加到,现在它添加,因为我使用路由器渲染家,如果没有选择路由。谢谢我的回答。请您重新检查代码沙盒,我最初忘记将添加到,现在添加了,因为如果没有选择任何路由,我将使用路由器渲染家庭。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢。谢谢