简体   繁体   English

反应本机 useState 不会重新渲染

[英]React native useState doesn't rerender

I was following Mosh's react native course when I ran into this problem after trying to delete item from a flatlist using the useState hook it doesn't rerender is there any thing wrong in this code or this is some sort of bug I have I would really appreciate the help this is my code and I'm using handleDelete as an onPress event:当我尝试使用 useState 钩子从平面列表中删除项目后遇到此问题时,我正在关注 Mosh 的 react native 课程,它不会重新呈现此代码中是否有任何问题,或者这是我遇到的某种错误,我真的会感谢帮助,这是我的代码,我使用 handleDelete 作为 onPress 事件:

const [messages, setMessages] = useState(initialMessages);

const handleDelete = (message) => {
    setMessages(messages.filter(m => m.id != message.id));
}

Edit: I've used mosh's source code but looks like it still doesn't work so I'll try to reinstall react then try again编辑:我使用了 mosh 的源代码,但看起来它仍然无法正常工作,所以我会尝试重新安装 react 然后再试一次

I've managed to make it work我已经成功了

so Basically I was using react native gesture handler after reinstalling it and expo / react-native I was able to make it work!所以基本上我在重新安装它和expo / react-native后使用了react native手势处理程序,我能够让它工作!

const [messages, setMessages] = useState(initialMessages);

const handleDelete = (message) => {
    setMessages(messages.filter(m => m.id != message.id));
}

useEffect(()=>{
handleDelete();
},[messages])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM