【问题标题】:React Native - state hook updates and it's re-rendering the component, but nothing showsReact Native - 状态钩子更新并重新渲染组件,但没有显示
【发布时间】:2021-02-05 12:41:30
【问题描述】:

代码如下:

export default () => {
    const [albums, setAlbums] = useState([]);
    useEffect(() => {
        MediaLibrary.getAlbumsAsync().then((tmpAlbums) => {
            setAlbums(tmpAlbums);
        });
    }, []);
    return (
        <View>
            {albums && (
                <FlatList
                    data={albums}
                    renderItem={({ item }) => {
                        <Text>{item.title}</Text>;
                    }}
                />
            )}
        </View>
    );
};

我确定状态会更新,因为我记录了它并且它已更新,我已经拥有权限,为了简单起见我刚刚删除了它。我已经尝试了所有方法,但组件/屏幕上没有显示任何内容。

【问题讨论】:

  • 错误似乎不在这里。你能分享你的整个代码吗,例如codesandbox.io

标签: reactjs react-native react-native-flatlist use-state


【解决方案1】:

您没有返回Text

随便

{albums && (
   <FlatList
     data={albums}
     renderItem={({ item }) => {
       return (<Text>{item.title}</Text>)
     }}
   />
 )}

或者

{albums && (
  <FlatList
    data={albums}
    renderItem={({ item }) => (
      <Text>{item.title}</Text>)
    )}
   />
 )}

【讨论】:

    猜你喜欢
    • 2019-09-23
    • 2021-12-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多