简体   繁体   English

handleAddOption (app.js:64) 处未定义的道具

[英]props of undefined at handleAddOption (app.js:64)

I have in my code a simple form where I can alert input data.我的代码中有一个简单的表单,我可以在其中提醒输入数据。 So In my first class, I define a function.所以在我的第一个 class 中,我定义了一个 function。 Then I pass that function to another class.然后我将 function 传递给另一个 class。

class IndecisionApp extends React.Component {
    constructor(props){
        super(props)
        this.handleAddOption = this.handleAddOption.bind(this)
        this.state = {
            options: ['test1,test2,test3']
        }
    }

    handleAddOption(option) {
        console.log(option)
    }

    render(){
        return (
            <div>
                <AddOption
                    handleAddOption={this.handleAddOption} 
                 />
            </div>
        )
    }
}

class AddOption extends React.Component {

    handleAddOption(e) {
        e.preventDefault()

        const option = e.target.elements.option.value.trim()

        if(option) {
         this.props.handleAddOption(option)
        }
    }

    render(){
        return(
            <div>
                <form onSubmit={this.handleAddOption}>
                    <input type="text" name="option"/>
                    <button>Add Option</button>
                </form>
            </div>
        )
    }
}

 ReactDOM.render(<IndecisionApp />, document.getElementById('app'))

This line of code this.props.handleAddOption(option) gives me the error.这行代码this.props.handleAddOption(option)给了我错误。

Uncaught TypeError: Cannot read property 'props' of undefined
at handleAddOption (app.js:64)
at HTMLUnknownElement.callCallback (react-dom.development.js:1527)
at Object.invokeGuardedCallbackDev (react-dom.development.js:1566)
at Object.invokeGuardedCallback (react-dom.development.js:1423)
at Object.invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:1437)
at executeDispatch (react-dom.development.js:1660)
at Object.executeDispatchesInOrder (react-dom.development.js:1682)
at executeDispatchesAndRelease (react-dom.development.js:2197)
at executeDispatchesAndReleaseTopLevel (react-dom.development.js:2208)
at Array.forEach (<anonymous>)
at forEachAccumulated (react-dom.development.js:2174)
at Object.processEventQueue (react-dom.development.js:2367)
at runEventQueueInBatch (react-dom.development.js:2379)
at Object.handleTopLevel [as _handleTopLevel] (react-dom.development.js:2389)
at handleTopLevelImpl (react-dom.development.js:2028)
at batchedUpdates (react-dom.development.js:14309)
at performFiberBatchedUpdates (react-dom.development.js:1874)
at stackBatchedUpdates (react-dom.development.js:1865)
at batchedUpdates (react-dom.development.js:1879)
at Object.batchedUpdatesWithControlledComponents [as batchedUpdates] (react-dom.development.js:1892)
at dispatchEvent (react-dom.development.js:2102)

I am binding the handleAddOption inside the constructor, so I don't know why I am getting that error.我在构造函数中绑定了handleAddOption ,所以我不知道为什么会出现这个错误。 What did I miss?我错过了什么?

Thanks, Theo.谢谢,西奥。

You need to add the constructor() inside the <AddOption /> , too.您还需要在<AddOption />中添加constructor()

class AddOption extends React.Component {
    constructor(props){
        super(props)
        this.handleAddOption = this.handleAddOption.bind(this)
        ...
    }
...

You need to bind handleAddOption of class AddOption to this:您需要将 class AddOption 的 handleAddOption 绑定到此:

class AddOption extends React.Component {
  constructor(props) {
    super(props);
    this.handleAddOption = this.handleAddOption.bind(this)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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