reactjs - 从 ReactDropdown 组件中获取选定的值
问题描述
我正在使用“react-dropdown”中的 ReactDropdown 在我的应用程序中创建各种下拉菜单。我在尝试从我的应用程序中的此组件中获取所选值时遇到问题。这是我尝试过的,但它给了我这个错误:TypeError:无法读取未定义的属性“值”
import React, { Component } from "react";
import ReactDropdown from "react-dropdown";
import 'react-dropdown/style.css';
import Checkbox from 'react-simple-checkbox';
class Reactor extends Component{
state = {
substrate: null,
size: null,
description: null
}
substrates = ['Potato', 'Carrots', 'Celery', 'Something'];
handleDescription = (e) => {
console.log("Worked (Description)")
console.log(e.target.value)
this.setState({
description: e.target.value
})
}
handleSizeDropdown = (e) => {
console.log("Worked (Size)")
console.log(e.target.value)
this.setState({
size: e.target.value()
})
}
handleSubstrateDropdown = (e) => {
console.log("Worked (Substrate)")
console.log(e.value)
this.setState({
substrate: e.target.value})
}
render() {
return (
<div className={"card container col-12 col-lg-4 login-card mt-2 "}>
<form>
<label className={"m-2"}>Select Substrate: </label>
<ReactDropdown options={this.substrates} onChange={this.handleSubstrateDropdown.bind(getSelection())} /><br></br>
<label className={"m-2"}>Select Particulate Size: </label>
<ReactDropdown options={['Small','Medium', 'Large']} onChange={this.handleSizeDropdown.bind(getSelection())} /><br></br>
<label className={"m-2"}>Reactor Description: </label>
<textarea className="form-control" type={"text"} onChange={this.handleDescription} rows={"3"} /><br></br>
</form>
</div>
);
}
}
export default Reactor;
我可以做些什么来访问选定的值?我使用 React Developer Tools 调试的是 ReactDropdown 的状态变量有一个“选定的”javascript 对象,其值如下:
{
"selected": {
"label": "Select...",
"value": ""
},
"isOpen": false
}
解决方案
我相信你只需要this
在构造函数中为你的事件处理程序添加绑定......
constructor(props) {
super(props);
this.state = {
substrate: null,
size: null,
description: null
}
this.handleSubstrateDropdown = this.handleSubstrateDropdown.bind(this);
this.handleSizeDropdown = this.handleSizeDropdown.bind(this);
this.handleDescription = this.handleDescription.bind(this);
}
此外,您没有正确设置状态。而是this.state = { //stuff }
在构造函数内部使用。并用于this.setState()
在构造函数之外修改状态。
constructor(props) {
super(props);
this.state = {
substrate: null,
size: null,
description: null
}
此外,您似乎正试图this.state.substrates
通过仅使用不起作用的方法来访问substrates = ...
。您需要使用this.setState()
来更改 中项目的值this.state
。
this.setState({substrates : ['Potato', 'Carrots', 'Celery', 'Something']};
你能试试这些改变吗...
import React, {
Component
} from "react";
import ReactDropdown from "react-dropdown";
import 'react-dropdown/style.css';
import Checkbox from 'react-simple-checkbox';
class Reactor extends Component {
constructor(props) {
super(props);
this.state = {
substrate: null,
size: null,
description: null
}
this.handleSubstrateDropdown = this.handleSubstrateDropdown.bind(this);
this.handleSizeDropdown = this.handleSizeDropdown.bind(this);
this.handleDescription = this.handleDescription.bind(this);
}
this.setState({substrates : ['Potato', 'Carrots', 'Celery', 'Something']};
handleDescription = (e) => {
console.log("Worked (Description)")
console.log(e.target.value)
this.setState({
description: e.target.value
})
}
handleSizeDropdown = (e) => {
console.log("Worked (Size)")
console.log(e.target.value)
this.setState({
size: e.target.value()
})
}
handleSubstrateDropdown = (e) => {
console.log("Worked (Substrate)")
console.log(e.value)
this.setState({
substrate: e.target.value
})
}
render() {
return ( <
div className = {
"card container col-12 col-lg-4 login-card mt-2 "
} >
<
form >
<
label className = {
"m-2"
} > Select Substrate: < /label> <
ReactDropdown options = {
this.substrates
}
onChange = {
this.handleSubstrateDropdown.bind(getSelection())
}
/><br></br >
<
label className = {
"m-2"
} > Select Particulate Size: < /label> <
ReactDropdown options = {
['Small', 'Medium', 'Large']
}
onChange = {
this.handleSizeDropdown.bind(getSelection())
}
/><br></br >
<
label className = {
"m-2"
} > Reactor Description: < /label> <
textarea className = "form-control"
type = {
"text"
}
onChange = {
this.handleDescription
}
rows = {
"3"
}
/><br></br >
<
/form> <
/div>
);
}
}
推荐阅读
- docker - 在 Dockerfile 中临时缓存 RUN apt-get update && apt-get install
- autodesk-navisworks - 是否有对 nwCreate API 的 .NET 支持?
- angular - 在 Ionic 4、Angular 7 项目中查找所有已弃用的代码
- python - Pygame:blit的目标位置无效
- vue.js - Vue 或 Webpack 编译的 JS 缓存管理
- c++ - 动态数组的实现
- xml - 如何通过 xml 配置 spring security 5.4.1 以使 oauth2 注销正常工作?
- python - Jupyter / Python — 有没有办法在每个 Jupyter 单元中自动运行 %%time 之类的魔术命令?
- python - Discord.py 响应反应
- c++ - 如何限制 rand 函数可以生成的数字。(分段故障)