[英]React Native: renderRow not executed
In the following code, when setState
is called from campaignsUpdated
, render
gets logged to the console, but not renderRow
: 在以下代码中,当从campaignsUpdated
调用setState
时, render
会记录到控制台,但不会记录到renderRow
:
var React = require('react-native'),
Bus = require('../Bus'),
styles = require('../Styles'),
CampaignsStore = require('../stores/Campaigns'),
CampaignItem = require('./CampaignItem'),
{
Component,
Text,
TextInput,
ListView,
View,
NavigatorIOS,
ActivityIndicatorIOS
} = React
class CampaignList extends Component {
constructor(props) {
super(props)
this.state = {
dataSource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
}
}
componentDidMount() {
this.addListeners()
Bus.emit('campaigns:search', '')
}
componentWillUnmount() {
this.removeListeners()
}
render() {
console.log('render')
return (
<View style={styles.container}>
<TextInput
style={styles.searchInput}
placeholder='Campaign Name'
value={this.state.campaignName}
onChange={this.campaignSearchChanged.bind(this)}/>
<ListView
dataSource = {this.state.dataSource}
renderRow = {this.renderRow.bind(this)}/>
</View>
)
}
renderRow(campaign) {
console.log('renderRow')
return <CampaignItem campaign={campaign}/>
}
addListeners() {
Bus.on({
'campaigns:updated': this.campaignsUpdated.bind(this)
})
}
removeListeners() {
Bus.off({
'campaigns:updated': this.campaignsUpdated.bind(this)
})
}
campaignsUpdated(event) {
var campaigns = event.data
this.setState({
dataSource: this.state.dataSource.cloneWithRows(campaigns)
})
}
campaignSearchChanged(event) {
var campaignName = event.nativeEvent.text
Bus.emit('campaigns:search', campaignName)
}
}
module.exports = CampaignList
What am I doing wrong here? 我在这做错了什么?
You are passing ListView
a function renderRow
that returns a component. 您正在向ListView
传递一个返回组件的函数renderRow
。 You would have to call that function within ListView
once it is passed, presumably during a map over campaigns
. 一旦传递了ListView
,就必须在ListView
调用该函数,大概是在campaigns
的地图上。
By the looks of it the most likely case is that you have a classic React mutability issue here. 从它的外观来看,最可能的情况是你在这里有一个经典的React可变性问题。
Ie I suspect your 'campaignsUpdated' method is called with either the same Array instance it received last time, or the elements within the list are the same instances. 即我怀疑你的'campaignsUpdated'方法是使用上次收到的相同Array实例调用的,或者列表中的元素是相同的实例。
Try using: 尝试使用:
campaignsUpdated(event) {
var campaigns = event.data.slice(); // <-- clone the array
this.setState({
dataSource: this.state.dataSource.cloneWithRows(campaigns)
})
}
If that doesn't work, then you either make the part that manages your list of compaigns create new copies when changes are made (eg const clone = {...campaign, title:"A new Title"}
) or update your rowHasChanged method to see if the title (or whatever data you need) has actually changed. 如果这不起作用,那么您可以使管理您的比较列表的部分在进行更改时创建新副本(例如const clone = {...campaign, title:"A new Title"}
)或更新您的rowHasChanged查看标题(或您需要的任何数据)是否实际发生变化的方法。
Here are two really good videos about immutability in JavaScript here: https://egghead.io/lessons/javascript-redux-avoiding-array-mutations-with-concat-slice-and-spread https://egghead.io/lessons/javascript-redux-avoiding-object-mutations-with-object-assign-and-spread 这里有两个关于JavaScript不变性的非常好的视频: https : //egghead.io/lessons/javascript-redux-avoiding-array-mutations-with-concat-slice-and-spread https://egghead.io/lessons / JavaScript的终极版回避对象突变与-对象指派和扩频
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.