我的應用程序有一個列表視圖,用於呈現項目列表。如何訪問React Native中父組件中的子組件值(在ListView中)
我的子組件的每點擊觸發狀態值
{ selected: !this.state.selected }
在圖像的變化上面我選擇了2個個子項目,我怎麼可以訪問我的父組件自己的價值?
例如,
Big Data: true
IoT: true
這是我的父母片段,Hosted on Github too
import InterestItem from './InterestItem';
class AddInterest extends Component {
componentWillMount() {
this.createDataSource(this.props);
}
componentWillReceiveProps(nextProps) {
this.createDataSource(nextProps);
}
createDataSource({ items }) {
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.dataSource = ds.cloneWithRows(items);
}
//return arrays of event from events
renderRow(item) {
return <InterestItem item={item} icon={computer} />;
}
render() {
const { centerEverything, skeleton, container, textContainer, contentContainer, listViewContainer,
titleContainer, descContainer, title, desc, submitContainer, submitTitle } = styles;
return (
<View style={[container]}>
<View style={[centerEverything, textContainer]}>
<View style={titleContainer}>
<Text style={[title]}>What kind of events are you interest in?</Text>
</View>
<View style={descContainer}>
<Text style={[desc]}>You'll see more events from the categories you choose.</Text>
</View>
</View>
<View style={[contentContainer]}>
<ListView
enableEmptySections
contentContainerStyle={listViewContainer}
dataSource={this.dataSource}
renderRow={this.renderRow}
/>
</View>
<View style={[centerEverything, {paddingBottom: 10}]}>
<View style={[centerEverything, submitContainer]}>
<Text style={submitTitle}>Submit</Text>
</View>
</View>
</View>
)
}
}
這是我的孩子組成,Hosted on GitHub too
class InterestItem extends Component {
state = {
selected: false
}
render() {
const { skeleton, centerEverything, container, textStyle } = styles;
return(
<TouchableWithoutFeedback onPress={() => this.setState({ selected: !this.state.selected })}>
<View style={[centerEverything, container, { backgroundColor: this.state.selected ? '#635eb4' : '#e7e7e7'}]}>
{this.props.icon}
<Text style={textStyle}>{this.props.item[0]}</Text>
</View>
</TouchableWithoutFeedback>
)
}
}
在從@freesoul要求,這裏的兒童實例(考慮到我有8個孩子)
考慮一個'InterestItem'實例,可以是'this.props.item'的值是什麼?你能舉一個例子嗎? –
@ free-soul我已經添加了所需的信息 –
我的建議是,你應該保持在你父母的孩子的狀態;像'state = {cars:false,bigData:true,hackintosh:false,iot:true,...}'。 –