Javascript React本机组件不更新
我对react native有一个我无法理解的问题,我可能有一个错误的方法 所以我有这个组件,基本上是一个菜单,当我点击一个项目时,我称之为handleClickJavascript React本机组件不更新,javascript,ajax,api,react-native,react-native-navigation,Javascript,Ajax,Api,React Native,React Native Navigation,我对react native有一个我无法理解的问题,我可能有一个错误的方法 所以我有这个组件,基本上是一个菜单,当我点击一个项目时,我称之为handleClick class Header extends Component { //constructor and ecc.... handleClick = (flow) => { navigate('Category', {catId: flow}) } render() { return (
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;
类头扩展组件{
//构造函数和ecc。。。。
handleClick=(流)=>{
导航('Category',{catId:flow})
}
render(){
返回(
{this.state.menu.map(menuSingle=>(
this.handleClick(menuSingle.object_id)}>
{menuSingle.title}
))}
);
}
}
导出默认标题;
然后在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;
类类别扩展组件{
//构造函数ecc。。。
componentDidMount(){
返回fetch(“url=“+parseInt(this.state.catId))
.then((response)=>response.json())
。然后((帖子)=>{
这是我的国家({
职位:职位,
加载:错误
},函数(){
});
})
.catch((错误)=>{
控制台错误(error);
});
}
render(){
if(this.state.loading){
返回(
加载。。。
)
}
返回(
{this.state.posts.map(post=>(
{post.title}
))}
);
}
}
导出默认类别;
关于如何从菜单组件中调用相同组件或更新类别状态,有什么建议吗?
谢谢请提供此类问题的示例您是否尝试将forceUpdate添加到setState的回调中?这可能会缩小问题的范围is@HaykShakhbazyan你是说在componentDidMount中吗?
this.setState({posts:posts,load:false},()=>this.forceUpdate())代码>