Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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
Reactjs 为什么函数作为子函数无效?_Reactjs_React Native - Fatal编程技术网

Reactjs 为什么函数作为子函数无效?

Reactjs 为什么函数作为子函数无效?,reactjs,react-native,Reactjs,React Native,我正在尝试将基于类的组件转换为功能性无状态组件。每次都会出现以下错误: 函数作为子函数无效。如果返回的是组件而不是 以下是我的组件: const ProfileStories = (props) => { const renderStory = () => { const data = props.userStory ? props.userStory.data : []; if (props.isClicked) { return (

我正在尝试将基于类的组件转换为功能性无状态组件。每次都会出现以下错误:

函数作为子函数无效。如果返回的是组件而不是

以下是我的组件:

 const ProfileStories = (props) => {
  const renderStory = () => {
    const data = props.userStory ? props.userStory.data : [];
    if (props.isClicked) {
      return (
        <FlatList
          data={data}
          renderItem={renderStoryItem}
          keyExtractor={(item, index) => index.toString()}
          numColumns={2}
        />
      );
    } return null;
  };

  const renderStoryItem = (item) => {
    return (
      <StoryItem
        key={item.id}
        content={item.content}
        date={item.createdAt}
      />
    );
  };

  return (
    <CenterView>
      {renderStory}
    </CenterView>
  );
};

export default ProfileStories;
const RenderStory = () => {
}
...
return (
    <CenterView>
      <RenderStory />
    </CenterView>
 );

和往常一样,必须使用语法呈现组件。要做到这一点,还必须将组件名称大写:

 const ProfileStories = (props) => {
  const renderStory = () => {
    const data = props.userStory ? props.userStory.data : [];
    if (props.isClicked) {
      return (
        <FlatList
          data={data}
          renderItem={renderStoryItem}
          keyExtractor={(item, index) => index.toString()}
          numColumns={2}
        />
      );
    } return null;
  };

  const renderStoryItem = (item) => {
    return (
      <StoryItem
        key={item.id}
        content={item.content}
        date={item.createdAt}
      />
    );
  };

  return (
    <CenterView>
      {renderStory}
    </CenterView>
  );
};

export default ProfileStories;
const RenderStory = () => {
}
...
return (
    <CenterView>
      <RenderStory />
    </CenterView>
 );
这与直接调用函数基本相同:

return (
  <CenterView>
    {renderStory()}
  </CenterView>
);

但是,您的内部函数在编写时并不是一个真正的组件。首先,它使用其父组件的道具。要使其成为真正的组件,您应该直接向其传递任何道具。目前它只是一个内部函数。

一如既往,必须使用语法呈现组件。要做到这一点,还必须将组件名称大写:

 const ProfileStories = (props) => {
  const renderStory = () => {
    const data = props.userStory ? props.userStory.data : [];
    if (props.isClicked) {
      return (
        <FlatList
          data={data}
          renderItem={renderStoryItem}
          keyExtractor={(item, index) => index.toString()}
          numColumns={2}
        />
      );
    } return null;
  };

  const renderStoryItem = (item) => {
    return (
      <StoryItem
        key={item.id}
        content={item.content}
        date={item.createdAt}
      />
    );
  };

  return (
    <CenterView>
      {renderStory}
    </CenterView>
  );
};

export default ProfileStories;
const RenderStory = () => {
}
...
return (
    <CenterView>
      <RenderStory />
    </CenterView>
 );
这与直接调用函数基本相同:

return (
  <CenterView>
    {renderStory()}
  </CenterView>
);
但是,您的内部函数在编写时并不是一个真正的组件。首先,它使用其父组件的道具。要使其成为真正的组件,您应该直接向其传递任何道具。目前它只是一个内部函数。

此处:

return (
  <CenterView>
    {renderStory}
  </CenterView>
);
您正在传递renderStory,它实际上是一个函数。您可能打算将RenderStore函数的结果作为子对象传递给CenterView,如下所示:

return (
  <CenterView>
    {renderStory()}
  </CenterView>
);
在这里:

您正在传递renderStory,它实际上是一个函数。您可能打算将RenderStore函数的结果作为子对象传递给CenterView,如下所示:

return (
  <CenterView>
    {renderStory()}
  </CenterView>
);
我的答案是:

您面临的问题是,您正在作为子级传递内部函数renderStory。React所期望的是jsx组件或null:这是函数返回的内容。您应该做的只是明确地称之为:

 <CenterView>
   {renderStory()}
 </CenterView>
我的答案是:

您面临的问题是,您正在作为子级传递内部函数renderStory。React所期望的是jsx组件或null:这是函数返回的内容。您应该做的只是明确地称之为:

 <CenterView>
   {renderStory()}
 </CenterView>

正如两个答案所说,您需要执行函数。JSX语法相当于运行组件函数,而不是将其作为值传递,这就是您在这里所做的,就像两个答案所说的,您需要执行该函数。JSX语法相当于运行组件函数,而不是将其作为值传递,这就是您在这里要做的