1
Good night!
I am trying to make a select component that is used more than once within another Parent component.
To create the "option" tags, I map inside the array I created as a state of the Parent/App class.
So far, so good. However, when I select an option, he understands that I changed the state that was set to an array, as a single value that in this case would be the value I selected.
Follows code:
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor (props) {
super(props)
this.state = {
value: '?',
//list
typeOfSearch: [
{
value: 'animated',
name: 'animated'
},
{
value: 'debuts',
name: 'debuts'
},
{
value: 'playoffs',
name: 'playoffs'
},
{
value: 'teams',
name: 'teams'
}
],
//timeframe
period: [
{
value: 'week',
name: 'week'
},
{
value: 'month',
name: 'month'
},
{
value: 'year',
name: 'year'
},
{
value: 'ever',
name: 'ever'
}
],
//timeframe
amountResults: [
{
value: '10',
name: '10'
},
{
value: '20',
name: '20'
},
{
value: '30',
name: '30'
},
],
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value,
});
console.log(event.target.value);
}
render() {
const createItem = (item, key) => <option key = {key} value = {item.value}>
{item.name} </option>;
return (
<div className="container">
<SelectFilter handleChange={this.handleChange} value={this.state.value} name="typeOfSearch">
{this.state.typeOfSearch.map(createItem)}
</SelectFilter>
<SelectFilter handleChange={this.handleChange} value={this.state.value} name="period">
{this.state.period.map(createItem)}
</SelectFilter>
<SelectFilter handleChange={this.handleChange} value={this.state.value} name="amountResults">
{this.state.amountResults.map(createItem)}
</SelectFilter>
</div>
);
}
}
class SelectFilter extends Component {
render(){
return (<select onChange={this.props.handleChange} value={this.props.value} name= {this.props.name}>
{this.props.children}
</select>)
}
}
export default App;
I really appreciate any help!
Hello Sergio! You always the savior of the homeland. Thank you for your return. The problem is that this way, if I prompt the 3 selects, when selecting one, the other goes back to the first value, as if the same value that I pass to a select, was replicated to the 3. I wanted to change the select that was selected, without changing the value of the other 2.
– Aline Vianna
In the method
handleChange
, I made a switch using the "name" property of each select. I created 3 empty States, one for each type of property, and 3 cases changing the value of each state:handleChange(event) { 
 switch (event.target.name) { 
 case "typeOfSearch": 
 this.setState({ 
 typeOfSearchSelected: event.target.value, 
 }); 
 break;
I don’t think it looks very elegant, but it worked.– Aline Vianna
@Alinevianna had not noticed that you were having 1 state for 3 selects... this is not a good idea :) I added a suggestion with an example. Take a look :)
– Sergio
was great! I will debug the code calmly to understand better what you did. Thank you so much for your help once again. ;)
– Aline Vianna