【问题标题】:update a parameter in a flatlist in react native在本机反应中更新平面列表中的参数
【发布时间】:2020-11-26 09:11:06
【问题描述】:

嗨,所以我目前有 2 个屏幕,屏幕 A 和屏幕 B。屏幕 A 有一个包含所有帖子的平面列表,当我按下特定帖子时,我导航到屏幕 B,这是该帖子的详细信息。

在屏幕 B 中,我添加了一个点赞图标,它会根据当前用户是否喜欢该帖子来更改填充或勾勒的时间。如果当前用户点赞了一篇文章,则该赞的记录会存储在我的数据库中。

我的问题是,当用户喜欢屏幕 B 上的帖子时,心脏会被填满,但是当我关闭屏幕 B 并转到屏幕 A,然后再次转到同一帖子的屏幕 B 时,心脏仍然被勾勒出来。只有当我重新加载我的应用程序时,它才会变为已填充。

屏幕 A:

  function PostsScreen({navigation}) {

  const [posts, setPosts] = useState([]);
  const [error, setError] = useState(false);
  const [loading, setLoading] = useState(false);

  const loadPosts = async () => {
  setLoading(true);
  const response = await postsApi.getPosts(); //Api call to get all posts from backend
  setLoading(false);

 if (!response.ok) return setError(true);

 setError(false);
 setPosts(response.data);
 };

useEffect(() => {
loadPosts();
 }, []);

return(
<ActivityIndicator visible={loading} />
<FlatList
  data={posts}
  keyExtractor={(post) => post.id.toString()}
  renderItem={({ item }) => (
    <Card
      title={item.title}
      subTitle={item.subTitle}
      onPress={() => 
      navigation.navigate(routes.POST_DETAILS,item)}
    />
   )}
 />
);
}

屏幕B

function PostDetailsScreen({ route }) {
const post = route.params;
const { user} = useAuth();

const [addedToLikes, setAddedToLikes] = useState(post.isLiked);
const[likesCount,setLikesCount]=useState(post.likesCount)

const addToLikes = (PostId,userId) => {
postsApi.likePost({PostId,userId}); //api call to save like in backend
setAddedToLikes(!addedToLikes);
};

let show_likes="";
if(addedToLikes){
show_likes=(likesCount >1)?(("Liked by you")+" and "+(likesCount - 1)+((likesCount ==2)?( " 
other"):(" others"))):("Liked by you");
}else if(likesCount >0){
show_likes=(likesCount ==1)?(likesCount+ " like"):(likesCount + " likes");
}

return(
<TouchableOpacity onPress={() => {addToLikes(post.id,user.id)}}>
          {addedToLikes?<MaterialCommunityIcons
          name="heart"
        />:<MaterialCommunityIcons
            name="heart-outline"
          />}
</TouchableOpacity>

<View><TextInput>{show_likes}</TextInput></View>

)}

我没有使用上下文或 redux。

如果有人可以帮助您,将不胜感激。如果需要更多代码,请告诉我。

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    您可以将indexcallback 发送到post detail 屏幕

    renderItem={({ item,index }) => (
        <Card
          title={item.title}
          subTitle={item.subTitle}
          onPress={() => 
          navigation.navigate(routes.POST_DETAILS,{post:item,index,updateView})}
        />
       )}
    

    使用回调函数更新list

    function PostDetailsScreen({ route }) {
    const post = route.params.post;
    const index = route.params.index;
    const updateView = route.params.updateView;
    const { user} = useAuth();
    
    const [addedToLikes, setAddedToLikes] = useState(post.isLiked);
    const[likesCount,setLikesCount]=useState(post.likesCount)
    
    const addToLikes = (PostId,userId) => {
    postsApi.likePost({PostId,userId}); 
    setAddedToLikes(!addedToLikes);
    updateView(!addedToLikes,index); // update main view
    };
    

    在首屏添加功能并更新列表

    const updateView = (isLiked, index) => {
      const newPosts = [...posts];
      newPosts[index].isLiked = isLiked;
      let currentCount = newPosts[index].likesCount;
      currentCount = isLiked?currentCount+1:currentCount-1;
      newPosts[index].likesCount = currentCount;
      setPosts(newPosts);
    };
    

    【讨论】:

    • 这确实有效,非常感谢 Mehran。 likesCount 呢?
    • 我已经更新了我的答案。如果有帮助,请将其标记为正确。
    • 嗨,希望你一切都好。你能帮我看看这个问题吗?我试图通过更新最后一条显示的消息来达到同样的目的。stackoverflow.com/questions/65759907/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2022-06-17
    • 2020-11-20
    • 2022-12-11
    • 2021-11-23
    • 2021-11-27
    相关资源
    最近更新 更多