简体   繁体   English

如何使用 React Native 导航从 FlatList 组件导航到扩展组件

[英]How to use react native navigation to navigate from a FlatList component to a expanded component

I am creating a basic blog application using ReactNative.我正在使用 ReactNative 创建一个基本的博客应用程序。 The home screen renders a FlatList of posts with a title, key, and author.主屏幕呈现带有标题、关键字和作者的帖子的 FlatList。 I want to be able to click on an individual post and navigate to a new screen that has the full blog post.我希望能够单击单个帖子并导航到包含完整博客帖子的新屏幕。 I will try and give as much code as possible to explain my problem.我会尝试提供尽可能多的代码来解释我的问题。

// ./Post
 function Post(props) {
  const navigation = useNavigation();
  return (
    <TouchableOpacity
      style={styles.container}
      onPress={() =>
        navigation.navigate({ name: "ExpandedPost", params: props.id })
      }
    >
      <View>
        <Text style={styles.post}>This is the title to a fake post</Text>
        <Text style={styles.text}>By {props.Author} </Text>
      </View>
      <Image
        source={{ uri: "https://picsum.photos/200/300" }}
        style={styles.thumbnail}
      />
    </TouchableOpacity>
  );
}

// ./ExpandedPost
export default function ExpandedPost({ navigation, route }) {

  return (
    <View style={styles.container}>
      <View>
        <Text style={styles.post}>This is the title to a fake post</Text>
        <Text> This is a body of  a fake post</Text>
      </View>
      <Image
        source={{ uri: "https://picsum.photos/200/300" }}
        style={styles.thumbnail}
      />
    </View>
  );
}

// ./PostList
 const RenderPosts = () => {
  return (
    <FlatList
      data={fakePosts}
      renderItem={({ item }) => <Post Author={item.Author} />}
    />
  );
};

export default function PostList() {
  return (
    <View style={styles.container}>
      <Header />
      <RenderPosts />
    </View>
  );
}

Basically, I want to take the post that is rendered in PostList, and onPress I want to navigate to ExpandedPost that contains all of the data from the specific post.基本上,我想获取在 PostList 中呈现的帖子,而 onPress 我想导航到包含来自特定帖子的所有数据的 ExpandedPost。

This might help这可能有帮助

// ./Post
 function Post(props) {
  const navigation = useNavigation();
  return (
    <TouchableOpacity
      style={styles.container}
      onPress={() =>
        navigation.navigate("ExpandedPost", {item: props.item})
      }
    >
      <View>
        <Text style={styles.post}>This is the title to a fake post</Text>
        <Text style={styles.text}>By {props.item.Author} </Text> <-- Change here -->
      </View>
      ...
    </TouchableOpacity>
  );
}

// ./ExpandedPost
export default function ExpandedPost(props) {
  
  const completeItemOfPost = props.item; <-- Complete Item Here --> 
  return (
    <View style={styles.container}>
      <View>
        <Text style={styles.post}>This is the title to a fake post</Text> <-- You can show title like "completeItemOfPost.title" -->
        <Text> This is a body of  a fake post</Text>  <-- You can show body like "completeItemOfPost.body" -->
      </View>
      <Image
        source={{ uri: "https://picsum.photos/200/300" }}  <-- You can show image like "completeItemOfPost.image" -->
        style={styles.thumbnail}
      />
    </View>
  );
}

// ./PostList
 const RenderPosts = () => {
  return (
    <FlatList
      data={fakePosts}
      renderItem={({ item }) => <Post item={item} />} <-- Pass complete item here... -->
    />
  );
};}

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

相关问题 如何在 React Native 中添加可点击的 FlatList 项并导航到 Detail 组件 - How to add clickable FlatList item and navigate to Detail component in React Native 如何在React Native中从绑定到平面列表的不同组件调用函数 - How to call function from different component bound to flatlist in React Native 如何从React Native组件导航到Native iOS UIViewController - How to Navigate from React Native Component to Native iOS UIViewController React Native - 如何在 FlatList 属性上使用导航? - React Native - How to use navigation on FlatList property? 如何在反应原生组件树中将一个组件导航到另一个组件? - How to navigate one component to other component in react native component tree? 如何在React本机组件之间导航? - How to navigate between React-native Component? 如何在 class 组件中使用导航? REACT-NATIVE - How can I use navigation in a class component? REACT-NATIVE 如何在 React Native 的一个功能组件中一起使用道具和导航 - How to use props and navigation together in one functional component in react native 如何使用 React 导航作为组件 - How to use React navigation as a component 在React Native FlatList中按下组件时如何更改其颜色 - How to change color of an component when its pressed in React Native FlatList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM