admin管理员组文章数量:1278720
I'm using react-select
. When I'm selecting a determinate value from select I've got the next issue
TypeError: Cannot read property 'value' of undefined
Also, values fetched from reducer todoList
are not showed, I can't see them.
This is my code:
import Select from "react-select";
import "./styles.css";
import { searchTodos } from "../../actions/ServiceActions";
class SelectedTodo extends Component {
constructor(props) {
super(props);
this.state = {
selectedTodo: ""
};
this.handleChange = this.handleChange.bind(this);
}
bringTodos = () => {
//WHEN I'M EXECUTING THESE CODE LINES I CAN'T SEE TODOS VALUES
return this.props.todoList.map(todo => {
return (
<option key={todo._id} value={todo._id}>
{todo.name}
</option>
);
});
};
handleChange = e => {
this.setState({ selectedTodo: e.target.value });
};
ponentDidMount() {
this.props.searchTodos();
}
render() {
const { loading } = this.props;
if (loading) {
return <span>...Loading</span>;
}
return (
<Select
className="form-container"
classNamePrefix="react-select"
placeholder="Please, insert a todo"
value={this.state.selectedTodo}
onChange={this.handleChange}
options={this.bringTodos()}
/>
);
}
}
function mapState(state) {
return {
todoList: state.todosDs.todoList
};
}
const actions = {
searchTodos
};
SelectedTodo = connect(
mapState,
actions
)(SelectedTodo);
export default SelectedTodo;
The expected behavior is dropdown shows todos and when I'm selecting a todo value I'm not getting error
TypeError: Cannot read property 'value' of undefined
I'm using react-select
. When I'm selecting a determinate value from select I've got the next issue
TypeError: Cannot read property 'value' of undefined
Also, values fetched from reducer todoList
are not showed, I can't see them.
This is my code:
import Select from "react-select";
import "./styles.css";
import { searchTodos } from "../../actions/ServiceActions";
class SelectedTodo extends Component {
constructor(props) {
super(props);
this.state = {
selectedTodo: ""
};
this.handleChange = this.handleChange.bind(this);
}
bringTodos = () => {
//WHEN I'M EXECUTING THESE CODE LINES I CAN'T SEE TODOS VALUES
return this.props.todoList.map(todo => {
return (
<option key={todo._id} value={todo._id}>
{todo.name}
</option>
);
});
};
handleChange = e => {
this.setState({ selectedTodo: e.target.value });
};
ponentDidMount() {
this.props.searchTodos();
}
render() {
const { loading } = this.props;
if (loading) {
return <span>...Loading</span>;
}
return (
<Select
className="form-container"
classNamePrefix="react-select"
placeholder="Please, insert a todo"
value={this.state.selectedTodo}
onChange={this.handleChange}
options={this.bringTodos()}
/>
);
}
}
function mapState(state) {
return {
todoList: state.todosDs.todoList
};
}
const actions = {
searchTodos
};
SelectedTodo = connect(
mapState,
actions
)(SelectedTodo);
export default SelectedTodo;
The expected behavior is dropdown shows todos and when I'm selecting a todo value I'm not getting error
Share Improve this question edited Mar 2, 2020 at 13:07 keikai 15.2k10 gold badges55 silver badges72 bronze badges asked Mar 2, 2020 at 12:30 CatMouse2020CatMouse2020 991 gold badge3 silver badges12 bronze badges 1TypeError: Cannot read property 'value' of undefined
- I think removing this.handleChange = this.handleChange.bind(this); and this.setState({selectedTodo: e}); will get you the selected option value – sourav satyam Commented Mar 2, 2020 at 12:42
2 Answers
Reset to default 9The package react-select
directly returns the json
that has the value like
{label:"ABC", value:"abc"}
Change this
handleChange = (e) =>{
this.setState({selectedTodo: e.target.value});
}
To this
handleChange = (e) =>{
this.setState({selectedTodo: e});
}
I think as the documentation of react-select says
<Select
value={selectedOption}
onChange={this.handleChange}
options={options}
/>
this.handleChange will directly return the selected option rather then event object.
handleChange = selectedOption => {
this.setState({ selectedOption });
console.log(`Option selected:`, selectedOption);
};
and I think you don't need to bind the handleChange method as well.
版权声明:本文标题:javascript - Using react-select I've got next issue: Cannot read property 'value' of undefined - Stack O 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741262870a2367945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论