【发布时间】:2020-10-27 19:21:43
【问题描述】:
我有一个组件,它在 componentDidMount 之后没有重新渲染。
渲染方法是这样的:
render() {
{
console.log("----------RENDERING-----------------------------")
}
return (
<ImageBackground
source={require('../assets/images/bg.png')}
style={styles.bg}
>
<View style={styles.containerHome}>
<View style={styles.top}>
<City/>
<Text>myapp</Text>
{/*<Filters />*/}
</View>
<CardStack
loop={true}
verticalSwipe={false}
renderNoMoreCards={() => null}
ref={swiper => (this.swiper = swiper)}
>
{this.state.data.map((item, index) => (
<Card key={index}>
<CardItem
text={item.name}
detail={item.detail}
imageurl={item.imageurl}
matches="0"
actions
onPressLeft={() => this.swiper.swipeLeft()}
onPressRight={() => this.swiper.swipeRight()}
/>
</Card>
))}
</CardStack>
</View>
</ImageBackground>
);
}
...只是渲染一个卡片组。
相关的是这一行:
this.state.data.map((item, index) => (
如果我从文件(演示)中设置数据静态,它可以工作!
表示线条是否像这样
Demo.map((item, index) => (
一切都好!
但是当我在 componentDidMount 中设置数据时,它不起作用! 我真的不知道 react-native 在这里做什么:
componentDidMount() {
this.setState({
isLoaded: true,
data: Demo
});
我将 state.data 设置为完全相同的演示值,但 react 没有重新渲染。
好像this.state.data一直是空的。
也许有人可以帮助我?
非常感谢
【问题讨论】:
-
Demo什么时候完成,总是静态导入吗?您是否实施了任何
shouldComponentUpdate?通常在状态更改时,react 会重新渲染所有内容,所以这应该可以工作,但它有一些警告。
标签: reactjs react-native react-native-android