this.props不传递功能

时间:2018-07-26 17:09:34

标签: javascript reactjs react-native react-navigation

在我的react本机文件中,我试图将一个函数传递给我的static navigationOptions中的子组件,但它一直说undefined is not a function(evaluating'_this.props.onUpdate(e.target.value)')我正在尝试从文本输入中传递一个值到父组件,但是它带来了上面的错误,请问我在做什么错了,

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

    );
}
}

父组件

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

1 个答案:

答案 0 :(得分:0)

static方法无权访问实例的this,因此,当您这样做时:

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

this.Updateundefined。因此,当您执行this.props.onUpdate(e.target.value);

它正在调用undefined(e.target.value),而undefined不是一个函数。