简体   繁体   中英

this.props not passing down function

In my react native file, I am trying to pass down a function to a child component in my static navigationOptions but it keeps on saying undefined is not a function(evaluating'_this.props.onUpdate(e.target.value)') I am trying to pass a the value from the text input to the parent component but it brings the error above please what am I doing wrong,

class Child extends React.Component {

constructor(props) {
    super(props);
    this.state = {
        search: ""
    }
}
handleChange = (e) => {
    this.props.onUpdate(e.target.value);
    this.setState({search: e.target.value});
};
render() {
    return (
        <TextInput placeholder="Search for your herbs by name,type..." value={this.state.search} onChange={this.handleChange}
                   underlineColorAndroid={'transparent'}
                   style={BackStyles.textBox}/>

    );
}
}

PARENT COMPONENT

export default class Parent extends React.Component {
Update = (val) => {
    this.setState({
        search: val
    });
    let db = SQLite.openDatabase({
        name: 'test.db',
        createFromLocation: "~Herbo.db",
        location: 'Library'
    }, this.openCB, this.errorCB);
    db.transaction((tx) => {
        tx.executeSql("SELECT * FROM data where name like '" + this.state.search + "' ", [], (tx, results) => {
            console.log("Query completed");
            var len = results.rows.length;
            for (let i = 0; i < len; i++) {
                let row = results.rows.item(i);
                this.setState(prevState => ({
                    record: [...prevState.record, row],
                    pressed: [...prevState.pressed, false]
                }));
                console.log(`Record: ${row.name}`);
                //this.sleep(2);
                //this.setState({record: row});
            }
            this.setState({loader: false})
        });
    });
};
static navigationOptions = ({navigation, navigationOptions}) => {
    const {params} = navigation.state;
    return {
         headerTitle: <Child onUpdate={this.Update}/>,
    };
};
constructor(props) {
    super(props);
    this.state = {
        record: [],
        header: null,
        loader: true,
        pressed: {},
        ar: [],
        search: ''
    };

    let db = SQLite.openDatabase({
        name: 'test.db',
        createFromLocation: "~Herbo.db",
        location: 'Library'
    }, this.openCB, this.errorCB);
    db.transaction((tx) => {
        tx.executeSql('SELECT * FROM data', [], (tx, results) => {
            console.log("Query completed");
            var len = results.rows.length;
            for (let i = 0; i < len; i++) {
                let row = results.rows.item(i);
                this.setState(prevState => ({
                    record: [...prevState.record, row],
                    pressed: [...prevState.pressed, false]
                }));
                console.log(`Record: ${row.name}`);
                //this.sleep(2);
                //this.setState({record: row});
            }
            this.setState({loader: false})
        });
    });
}

A static method doesn't have access to an instance's this , so when you do:

static navigationOptions = ({navigation, navigationOptions}) => {
   const {params} = navigation.state;
    return {
         headerTitle: <Child onUpdate={this.Update}/>,
    };
};

the this.Update is undefined . So when you do this.props.onUpdate(e.target.value);

it is calling undefined(e.target.value) , and undefined is not a function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM