React native 创建一个子类,子类可以在其中调用它';在React Native中的父类中的函数
假设我有以下代码作为我的子类:React native 创建一个子类,子类可以在其中调用它';在React Native中的父类中的函数,react-native,wrapper,React Native,Wrapper,假设我有以下代码作为我的子类: export class ChildComponent extends React.Component { constructor(props) { super(props); } render() { return ( <View style = {styles.horizontalView}> {this.props.children}
export class ChildComponent extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<View style = {styles.horizontalView}>
{this.props.children}
<TextInput
style={styles.textInput}
onChangeText = {() => {
someFunction()
//call someFunction() in a parent class
}}
/>
</View>
)
}
}
如果你有这个问题的解决方案,请告诉我。再次感谢您。试试这个
export class ParentComponent extends React.Component {
constructor(props) {
super(props);
}
//someFunction is called by the child but is defined here by the parent.
someFunction() {
}
render() {
<ChildComponent parentFunction={this.someFunction}/>
}
}
导出类ParentComponent扩展React.Component{
建造师(道具){
超级(道具);
}
//someFunction由子函数调用,但在此由父函数定义。
someFunction(){
}
render(){
}
}
然后在childComponent中
export class ChildComponent extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<View style = {styles.horizontalView}>
{this.props.children}
<TextInput
style={styles.textInput}
onChangeText = {() => {
//call someFunction() in a parent class
this.props.parentFunction();
}}
/>
</View>
)
}
}
导出类ChildComponent扩展React.Component{
建造师(道具){
超级(道具);
}
render(){
返回(
{this.props.children}
{
//在父类中调用someFunction()
this.props.parentFunction();
}}
/>
)
}
}
代码不显示“子类”。如果建立了父子类/类型关系,则该方法将可用于父类“和所有派生/子类”。
export class ChildComponent extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<View style = {styles.horizontalView}>
{this.props.children}
<TextInput
style={styles.textInput}
onChangeText = {() => {
//call someFunction() in a parent class
this.props.parentFunction();
}}
/>
</View>
)
}
}