-
-
Save EQuimper/d7c27f455b5bd10b2104cbe9789af115 to your computer and use it in GitHub Desktop.
React Native - ListView receiving data as a prop <ListView data={[1,2,3]} />
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { ListView } from 'react-native'; | |
export default class extends React.PureComponent { | |
constructor(props) { | |
super(props); | |
const { data, dataSource, rowHasChanged: _rowHasChanged } = props; | |
this.state.data = data || []; | |
const rowHasChanged = _rowHasChanged !== undefined ? _rowHasChanged : this.rowHasChanged; | |
const ds = dataSource || new ListView.DataSource({ rowHasChanged }); | |
this.state.dataSource = ds.cloneWithRows(this.state.data); | |
} | |
state = { | |
dataSource: null, | |
}; | |
componentWillReceiveProps({ data }) { | |
const { data: oldData } = this.props; | |
if (data !== oldData) { | |
const dataSource = this.state.dataSource.cloneWithRows(data || []); | |
this.setState({ dataSource }); | |
} | |
} | |
rowHasChanged = (r1, r2) => r1 !== r2; | |
props: { | |
data: Array, | |
dataSource?: ?Object, | |
rowHasChanged?: Function, | |
style?: ?Object, | |
contentContainerStyle?: ?Object, | |
renderRow: Function, | |
renderHeader?: Function, | |
renderFooter?: Function, | |
}; | |
render() { | |
const { ...props } = this.props; | |
delete props.dataSource; | |
return ( | |
<ListView | |
dataSource={this.state.dataSource} | |
enableEmptySections | |
{...props} | |
/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment