經過數小時的搜索後才發現沒有找到答案......我在尋求您的幫助。如何在Native React中的孩子內部調用父功能
所以我想要做的是:在子內調用名爲_toggleSearchBar()的函數(它在父類中),這樣當onPress事件(在子項中)觸發它時,會更改「isVisible」家長。
家長
class HomeScreen extends React.Component {
constructor(props) {
super(props);
this.state = {isVisible: false};
}
static navigationOptions = {
title: 'P O S T E R',
headerStyle: { backgroundColor: '#CECECE' },
headerTitleStyle: { color: 'black', fontSize: 30, fontFamily: 'HelveticaNeue-CondensedBlack'},
headerRight: <DisplayIcon src={require('./ressources/icon_search.png')} myMethod={'HERE'}/>,
headerLeft: <DisplayIcon src={require('./ressources/icon_aroundMe.png')}/>,
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<View style={styles.bck}>
<ScrollView>
<DisplayImage src={require('./ressources/logo.jpg')} />
<DisplayImage src={require('./ressources/logo1.jpeg')} />
<DisplayImage src={require('./ressources/logo2.jpg')} />
<DisplayImage src={require('./ressources/logo3.jpeg')} />
<DisplayImage src={require('./ressources/logo4.jpg')} />
<DisplayImage src={require('./ressources/logo5.jpeg')} />
<DisplayImage src={require('./ressources/bde.jpeg')} />
</ScrollView>
</View>
<Display enable={this.state.isVisible} style={styles.ViewIn}>
<View>
<TextInput style={styles.textIn}></TextInput>
</View>
</Display>
</View>
)
}
_toggleSearchBar() {
this.setState(previousState => {
return { isVisible: !this.state.isVisible };
});
}
}
兒童
class DisplayIcon extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<TouchableHighlight onPress={this.myMethod} activeOpacity= {0.4} underlayColor={ 'rgb(206, 206, 206)' }>
<Image style={styles.Picture} source={this.props.src}/>
</TouchableHighlight>
);
}
}
const styles = StyleSheet.create({
Picture: {
marginLeft: 10,
marginRight: 10,
height: 30,
width: 30,
}
});
綁定沒有工作。不通過道具傳遞功能...
感謝您的幫助和您的時間!
請不要發佈圖片的代碼。直接將您的代碼添加到問題中,以便幫助人們幫助您。 – bennygenel
好吧,我的壞 – Maxime
位於層次結構中的DisplayIcon組件在哪裏?因爲我在HomeScreen中看到的只是DisplayImage。 DisplayImage和DisplayIcon是否是相同的組件? –