Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 创建一个子类,子类可以在其中调用它';在React Native中的父类中的函数_React Native_Wrapper - Fatal编程技术网

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>
        )
    }
}