SegmentedControlIOS fails to switch between dynamic ListView
MacOSX 10.11.5 + React Native 0.28
Basically, I have a component ListEditor that has a dynamically-generated listview, I place 3 of this component on the page and switch between them using SegmentedControlIOS by predefining them. The problem is, when switching, the rendering engine doesn't see the difference and render the same list. I have to arbitrarily place a
'ListEditor.js' `'use strict'
import React, { Component, PropTypes } from 'react'
import { ListView, StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native'
class ListEditor extends Component { constructor(props) { super(props) this.createItem = this.createItem.bind(this) this.sendToEdit = this.sendToEdit.bind(this) this.saveText = this.saveText.bind(this) this.renderRow = this.renderRow.bind(this) this.dataSource = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 != r2 })
this.state = {
list: this.props.list || [],
listRenderData: this.dataSource.cloneWithRows(this.props.list || []),
selectedText: '',
selectedIndex: -1
}
}
renderRow(rowData, sectionId, rowId, highlightRow) { const rowText = rowData.toString()
return (
<TouchableOpacity key={rowId} onPress={() => {
this.sendToEdit(rowText, rowId)
highlightRow(sectionId, rowId)
}}>
<View style={[styles.row, (rowId === this.state.selectedIndex) ? styles.highlight : null]}>
<Text>{rowText}</Text>
</View>
</TouchableOpacity>
)
}
createItem() { this.setState({ selectedText: '', selectedIndex: -1 }) }
sendToEdit(text, index) { this.setState({ selectedText: text, selectedIndex: parseInt(index) }) }
saveText(text, index) { (index < 0) ? this.state.list.push(text) : this.state.list.splice(index, 1, text)
this.setState({
selectedText: text,
listRenderData: this.dataSource.cloneWithRows(this.state.list),
})
}
render() {
return (
class InputBar extends Component { constructor(props) { super(props) this.createItem = this.createItem.bind(this) this.onChange = this.onChange.bind(this) this.saveText = this.saveText.bind(this)
this.state = {
text: this.props.text || '',
index: this.props.index || -1,
height: 40
}
}
componentWillReceiveProps(nextProps) { this.setState({ text: nextProps.text, index: nextProps.index })
this.refs.textInput.focus()
}
saveText() { var tmp = this.state.text.trim() this.props.saveText(tmp, this.state.index) }
createItem() { this.setState({ text: '', index: -1, height: 40 }) }
onChange(evt) { var height = evt.nativeEvent.contentSize.height, height = (height > 80) ? 80 : Math.max(40, height)
this.setState({
text: evt.nativeEvent.text,
height: height
})
}
render() {
return (
InputBar.propTypes = { saveText: PropTypes.func.isRequired, text: PropTypes.string, index: PropTypes.number }
const styles = StyleSheet.create({ listEditor: { flex: 1, flexDirection: 'column', }, row: { backgroundColor: '#efe', borderBottomColor: '#ccc' borderBottomWidth: 1, justifyContent: 'flex-start', padding: 15 }, highlight: { backgroundColor: '#fcc', },
inputBar: { alignItems: 'center', backgroundColor: 'white', borderTopColor: '#ccc', borderTopWidth: 1, flexDirection: 'row', padding: 10, }, inputWrapper: { borderColor: '#ccc', borderRadius: 4, borderWidth: 1, flex: 1, overflow: 'hidden', }, textInput: { color: 'black', fontSize: 16, height: 44.5, paddingHorizontal: 15, paddingVertical: 10, }, inputButtonLeft: { flex: 0, paddingLeft: 5, paddingRight: 15, }, inputButtonRight: { flex: 0, paddingLeft: 15, paddingRight: 5, }, inputButtonText: { color: 'red', }, })
export default ListEditor
'main.js'
class App extends Component { constructor(props) { super(props)
this.state = {
selectedIndex: 0,
detail: ['1','2','3','4','5'],
include: ['A','B','C','D','E','F'],
exclude: ['a','b','c']
}
}
render() { var mainView = null
switch (this.state.selectedIndex) {
case 0:
mainView = (
<ListEditor ref='expDetail' list={this.state.detail} />
)
break
case 1:
mainView = (
<View> // <- this stupid hack should not be needed!
<ListEditor ref='expInclude' list={this.state.include} />
</View>
)
break
case 2:
mainView = (
<ScrollView>
<ListEditor ref='expExclude' list={this.state.exclude} />
</ScrollView>
)
break
}
return (
<View style={styles.detail.wrapper}>
<View style={styles.editor.scroll}>
<SegmentedControlIOS
values={"detail", "include", "exclude"]}
selectedIndex={this.state.selectedIndex}
onChange={(event) => {
this.setState({selectedIndex: event.nativeEvent.selectedSegmentIndex})
}}
style={{marginHorizontal: 15, marginBottom: 15}}
/>
<View style={{flex: 1}}>
{mainView}
</View>
</View>
</View>
)
} }`
This problem is solved by adding a arbitrary 'key' attribute and assign a random value to each ListEditor.