javascript - React 本机组件不更新
问题描述
我对本机反应有一个我无法理解的问题,我可能有一个错误的方法。
所以我有这个组件,基本上是一个菜单,当我单击一个项目时,我调用了 handleClick。
class Header extends Component {
//constructor and ecc....
handleClick = (flow) => {
navigate('Category', {catId: flow})
}
render() {
return (
<View>
{this.state.menu.map(menuSingle => (
<View>
<TouchableOpacity onPress={() => this.handleClick(menuSingle.object_id)} >
<Text>{menuSingle.title }</Text>
</TouchableOpacity>
</View>
))}
</View>
);
}
}
export default Header;
然后在 Category.js 中,我从菜单中按 id 获取数据,但如果使用菜单表单 home.js 组件它正在工作,但是当我在 category.js 中使用它时,相同的菜单不会更新状态。
class Category extends Component{
//constructor ecc...
componentDidMount(){
return fetch("url="+parseInt(this.state.catId))
.then((response) => response.json())
.then((posts) => {
this.setState({
posts: posts,
loading: false
}, function(){
});
})
.catch((error) =>{
console.error(error);
});
}
render() {
if(this.state.loading) {
return (
<Text> Loading...</Text>
)
}
return (
<SafeAreaView >
<Header/>
<ScrollView>
{this.state.posts.map(post => (
<View>
<Text> {post.title} </Text>
</View>
))}
</ScrollView>
</SafeAreaView>
);
}
}
export default Category;
有关如何从菜单组件中调用相同组件或更新类别状态的任何建议?谢谢
解决方案
推荐阅读
- mysql - ON DUPLICATE KEY 语法问题
- r - gganimate 每个条形颜色的堆叠条形图
- list - 如何动态创建列表列表?
- c#-4.0 - C#如何在ListView_ItemDataBound的相同标签中获取先前的值?
- c++ - 左值对象使用带有右值引用的转换构造函数成功地从函数返回
- c++ - 下面给出的是 Cp3 书中的 c++ 代码。我无法理解这一行:-scanf("0.%[0-9]...\n", &x);
- google-sheets-api - 工作表/密钥/公共/完整服务需要从 30.04.2019 开始的 NID cookie
- rust - 为什么没有发生借用重叠时会出现借用错误?
- python - 无法将数据写入 RDD
- c# - 需要将多个文本框的 TextBox_LostFocus 的重复代码缩减为一个函数