【发布时间】:2018-09-07 00:05:05
【问题描述】:
我无法弄清楚为什么我的应用程序进行无休止的渲染。
在里面,我的有状态组件,我在componentDidMount方法中调用了一个redux动作(调用componentWillMount也做无限渲染)
class cryptoTicker extends PureComponent {
componentDidMount() {
this.props.fetchCoin()
// This fetches some 1600 crypto coins data,Redux action link for the same in end
}
render() {
return (
<ScrollView>
<Header />
<View>
<FlatList
data={this.state.searchCoin ? this.displaySearchCrypto : this.props.cryptoLoaded}
style={{ flex: 1 }}
extraData={[this.displaySearchCrypto, this.props.cryptoLoaded]}
keyExtractor={item => item.short}
initialNumToRender={50}
windowSize={21}
removeClippedSubviews={true}
renderItem={({ item, index }) => (
<CoinCard
key={item["short"]}
/>
)}
/>
</View>
</ScrollView>
)
}
}
在 CoinCard 中,除此之外我什么都不做(注意平面列表中的 CoinCard)
class CoinCard extends Component {
render () {
console.log("Inside rende here")
return (
<View> <Text> Text </Text> </View>
)
}
}
现在,当我控制台登录我的 coincard 渲染时,我可以看到 Inside rende here 的无限日志
[问题:]谁能帮我弄清楚为什么会发生这种情况?
您可以click here to see my actions 和click here to see my reducer。
[更新:]我的repository is here,如果您想克隆并自己查看。
[更新:2]:我已经在github上推送了上面的共享代码,它仍然会记录无尽的console.log语句(如果你可以克隆,run and move back to this commit)。
[Update:3]: 我不再在<FlatList /> 中使用<ScrollView />,当我的意思是无限渲染时,我的意思是它是无限的(并且不必要地)将相同的道具传递给子组件(<Coincard />),如果我使用 PureComponent,它不会在render () { 中无休止地登录,而是在componentWillRecieveProps 中,如果我使用console.log(nextProps),我可以看到相同的日志一遍又一遍地传递
【问题讨论】:
-
没有足够的信心发布完整的答案,但我认为您的问题可能是here。当您更新该组件的道具时,我认为
componentDidUpdate会触发,这将再次更新道具等。这也会导致FlatList的data道具发生变化,这也会每次都渲染它,导致无限渲染。 -
请在问题中包含相关代码。
-
@izb 非常感谢您的回答!我删除了
componentDidUpdate,实际上我从代码中删除了所有内容并将其归结为我在上面共享的内容,但我仍然可以看到那些无穷无尽的日志:\ -
您的项目可能不止一个问题。具体来说,您永远不应该分配给道具github.com/irohitb/Crypto/blob/…
-
动作和减速器很好。如果我剥离 all 视图/列表/等,只渲染组件,并删除所有状态更新函数,所有内容只会渲染两次。这似乎是 React Native 特有的,我不是这方面的专家。我已在您的问题中添加了
react-native标签,因此希望这有助于找到合适的人。祝你好运,这似乎是一个非常棘手的问题。我怀疑您会想要使用虚拟化列表,重新渲染是由布局问题引起的。
标签: javascript reactjs react-native