【问题标题】:Uncaught TypeError: Cannot read property 'target'未捕获的类型错误:无法读取属性“目标”
【发布时间】:2020-03-29 23:01:25
【问题描述】:

未捕获的类型错误:无法读取未定义的属性“目标”

下拉处理程序:

handlePortfolioChange = data => (event) => {
    this.resetForm();
    this.setState({ selectedPortfolio: event.target.value });
}

实际选择框

<Select value={this.state.sp}
    onChange={e =>
    this.setState({sp: e.target.value
    }, this.handleChange(data, e))}
>

【问题讨论】:

  • 你打错了,应该是this.handleChange(data)(e)

标签: reactjs


【解决方案1】:

你有一个 curried 函数在那里(它需要多组参数)。在你的情况下dataevent

您可以像这样同时使用两个特定组参数调用它:handlePortfolioChange(data)(event)(而不是handlePortofolioChange(data,e))。

也许值得一提的是,您还可以使用特定的 data 对象定义一个更窄的函数,以便每次都使用,只需要像这样改变 event

//say, you had data-object that doesn't change in this scope like constantData
constantData = {
"some": 1,
"data": 2,
"here": "idk"
}

// then you could define a more particular function that handlePortfolioChange, by passing //only the first argument to the general function:
handleConstantPortfolioData = handlePortfolioData(constantData);

然后你可以在任何地方调用它,但只需要担心事件:

...onChange={e => this.handleConstantPortfolioData(e)}...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 2022-07-22
    • 2021-04-22
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多