ReactJS component state dropdown not updating
up vote
-1
down vote
favorite
I have a reactjs custom dropdown component which contains a list of mobiles.
Three instances of components get rendered initially.
I want to remove the selected mobile from other dropdowns
For example, let's say that the master mobile list contains Apple, Nokia, Samsung, and OnePlus.
If you select Nokia in first dropdown then in the second dropdown you should see the mobiles from the list but without Nokia.
- First Dropdown -- Nokia, Apple, Samsung, OnePlus (selected-->Nokia)
- Second Dropdown -- Apple, Samsung, OnePlus (selected-->Apple)
- Third Dropdown -- Samsung, OnePlus
reactjs
add a comment |
up vote
-1
down vote
favorite
I have a reactjs custom dropdown component which contains a list of mobiles.
Three instances of components get rendered initially.
I want to remove the selected mobile from other dropdowns
For example, let's say that the master mobile list contains Apple, Nokia, Samsung, and OnePlus.
If you select Nokia in first dropdown then in the second dropdown you should see the mobiles from the list but without Nokia.
- First Dropdown -- Nokia, Apple, Samsung, OnePlus (selected-->Nokia)
- Second Dropdown -- Apple, Samsung, OnePlus (selected-->Apple)
- Third Dropdown -- Samsung, OnePlus
reactjs
Did you do a web search? You can't be the first with that kind of problem.
– Robert
Nov 11 at 2:29
I try but didn't find the solution
– Ravi Mathpal
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have a reactjs custom dropdown component which contains a list of mobiles.
Three instances of components get rendered initially.
I want to remove the selected mobile from other dropdowns
For example, let's say that the master mobile list contains Apple, Nokia, Samsung, and OnePlus.
If you select Nokia in first dropdown then in the second dropdown you should see the mobiles from the list but without Nokia.
- First Dropdown -- Nokia, Apple, Samsung, OnePlus (selected-->Nokia)
- Second Dropdown -- Apple, Samsung, OnePlus (selected-->Apple)
- Third Dropdown -- Samsung, OnePlus
reactjs
I have a reactjs custom dropdown component which contains a list of mobiles.
Three instances of components get rendered initially.
I want to remove the selected mobile from other dropdowns
For example, let's say that the master mobile list contains Apple, Nokia, Samsung, and OnePlus.
If you select Nokia in first dropdown then in the second dropdown you should see the mobiles from the list but without Nokia.
- First Dropdown -- Nokia, Apple, Samsung, OnePlus (selected-->Nokia)
- Second Dropdown -- Apple, Samsung, OnePlus (selected-->Apple)
- Third Dropdown -- Samsung, OnePlus
reactjs
reactjs
edited Nov 11 at 4:06
Graham
3,296113558
3,296113558
asked Nov 10 at 14:40
Ravi Mathpal
643
643
Did you do a web search? You can't be the first with that kind of problem.
– Robert
Nov 11 at 2:29
I try but didn't find the solution
– Ravi Mathpal
2 days ago
add a comment |
Did you do a web search? You can't be the first with that kind of problem.
– Robert
Nov 11 at 2:29
I try but didn't find the solution
– Ravi Mathpal
2 days ago
Did you do a web search? You can't be the first with that kind of problem.
– Robert
Nov 11 at 2:29
Did you do a web search? You can't be the first with that kind of problem.
– Robert
Nov 11 at 2:29
I try but didn't find the solution
– Ravi Mathpal
2 days ago
I try but didn't find the solution
– Ravi Mathpal
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
You can either keep the options of your dropdown in state and maintain a different state for options of each dropdown
New contributor
add a comment |
up vote
0
down vote
I would approach it this way
1.Have a parent container that renders 3 drop-down children component.
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
child1: ['samsung', 'sony'],
child2: ['samsung', 'sony'],
child3: ['samsung', 'sony'],
}
}
render()
return(
<div> // see below for handleSelect function
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child1} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child2} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child3} />
<div>
)
}
2.In parent container, create a function that will get triggered when a child's dropdown option is selected. e.g.)
// In parent container
handleSelect(object) {
// Do logic here.
// e.g. if the first child selects an option by
if (object.selectedChild === 'FIRST')
const selected = object.selected;
// remove selected from an array
this.setState({ ... update the second dropdown selectables });
}
// In child component. Call this when option is selected
handleSelect(value) {
const obj = { selectedChild: 'FIRST' , selected: value };
this.props.onSelect(obj); // call parent function
}
3.When a child calls
this.props.onSelect(obj)
It will update the state in parent accordingly, which will pass down updated props to the children when it re-renders
Then child will get re-rendered with updated selectable object.
New contributor
I am able to updated the state but change in state value doesnt reflecting on dropdown control
– Ravi Mathpal
2 days ago
In child component, you can use componentDidUpdate to see if the props have changed. If the props from a parent is different than the previous pros meaning the drop down selectables have changed then you can try to re-render at the child component level. Please try it and see if it works.
– Junhouse
2 days ago
add a comment |
up vote
0
down vote
I can help you only with a state shape. Components handling should be your own work.
You can use a shared state for your dropdowns. Where you will to store an array of items and track selected item.
For example somewhere in a container:
state = {
data: [
{
name: 'Nokia',
selected: false,
},
{
name: 'Apple',
selected: false,
},
{
name: 'OnePlus',
selected: false,
}
]
}
Inside your dropdown component simple filter this array with .filter()
like this:
this.state.data.filter((item) => !item.selected)
Handle select logic and selected item for dropdown label should not be a problem.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can either keep the options of your dropdown in state and maintain a different state for options of each dropdown
New contributor
add a comment |
up vote
0
down vote
You can either keep the options of your dropdown in state and maintain a different state for options of each dropdown
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
You can either keep the options of your dropdown in state and maintain a different state for options of each dropdown
New contributor
You can either keep the options of your dropdown in state and maintain a different state for options of each dropdown
New contributor
New contributor
answered Nov 10 at 15:14
Prerna Chuttani
1
1
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
I would approach it this way
1.Have a parent container that renders 3 drop-down children component.
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
child1: ['samsung', 'sony'],
child2: ['samsung', 'sony'],
child3: ['samsung', 'sony'],
}
}
render()
return(
<div> // see below for handleSelect function
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child1} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child2} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child3} />
<div>
)
}
2.In parent container, create a function that will get triggered when a child's dropdown option is selected. e.g.)
// In parent container
handleSelect(object) {
// Do logic here.
// e.g. if the first child selects an option by
if (object.selectedChild === 'FIRST')
const selected = object.selected;
// remove selected from an array
this.setState({ ... update the second dropdown selectables });
}
// In child component. Call this when option is selected
handleSelect(value) {
const obj = { selectedChild: 'FIRST' , selected: value };
this.props.onSelect(obj); // call parent function
}
3.When a child calls
this.props.onSelect(obj)
It will update the state in parent accordingly, which will pass down updated props to the children when it re-renders
Then child will get re-rendered with updated selectable object.
New contributor
I am able to updated the state but change in state value doesnt reflecting on dropdown control
– Ravi Mathpal
2 days ago
In child component, you can use componentDidUpdate to see if the props have changed. If the props from a parent is different than the previous pros meaning the drop down selectables have changed then you can try to re-render at the child component level. Please try it and see if it works.
– Junhouse
2 days ago
add a comment |
up vote
0
down vote
I would approach it this way
1.Have a parent container that renders 3 drop-down children component.
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
child1: ['samsung', 'sony'],
child2: ['samsung', 'sony'],
child3: ['samsung', 'sony'],
}
}
render()
return(
<div> // see below for handleSelect function
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child1} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child2} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child3} />
<div>
)
}
2.In parent container, create a function that will get triggered when a child's dropdown option is selected. e.g.)
// In parent container
handleSelect(object) {
// Do logic here.
// e.g. if the first child selects an option by
if (object.selectedChild === 'FIRST')
const selected = object.selected;
// remove selected from an array
this.setState({ ... update the second dropdown selectables });
}
// In child component. Call this when option is selected
handleSelect(value) {
const obj = { selectedChild: 'FIRST' , selected: value };
this.props.onSelect(obj); // call parent function
}
3.When a child calls
this.props.onSelect(obj)
It will update the state in parent accordingly, which will pass down updated props to the children when it re-renders
Then child will get re-rendered with updated selectable object.
New contributor
I am able to updated the state but change in state value doesnt reflecting on dropdown control
– Ravi Mathpal
2 days ago
In child component, you can use componentDidUpdate to see if the props have changed. If the props from a parent is different than the previous pros meaning the drop down selectables have changed then you can try to re-render at the child component level. Please try it and see if it works.
– Junhouse
2 days ago
add a comment |
up vote
0
down vote
up vote
0
down vote
I would approach it this way
1.Have a parent container that renders 3 drop-down children component.
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
child1: ['samsung', 'sony'],
child2: ['samsung', 'sony'],
child3: ['samsung', 'sony'],
}
}
render()
return(
<div> // see below for handleSelect function
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child1} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child2} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child3} />
<div>
)
}
2.In parent container, create a function that will get triggered when a child's dropdown option is selected. e.g.)
// In parent container
handleSelect(object) {
// Do logic here.
// e.g. if the first child selects an option by
if (object.selectedChild === 'FIRST')
const selected = object.selected;
// remove selected from an array
this.setState({ ... update the second dropdown selectables });
}
// In child component. Call this when option is selected
handleSelect(value) {
const obj = { selectedChild: 'FIRST' , selected: value };
this.props.onSelect(obj); // call parent function
}
3.When a child calls
this.props.onSelect(obj)
It will update the state in parent accordingly, which will pass down updated props to the children when it re-renders
Then child will get re-rendered with updated selectable object.
New contributor
I would approach it this way
1.Have a parent container that renders 3 drop-down children component.
class Parent extends Component {
constructor(props) {
super(props);
this.state = {
child1: ['samsung', 'sony'],
child2: ['samsung', 'sony'],
child3: ['samsung', 'sony'],
}
}
render()
return(
<div> // see below for handleSelect function
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child1} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child2} />
<Dropdown onSelect={this.handleSelect.bind(this)} selectables={this.state.child3} />
<div>
)
}
2.In parent container, create a function that will get triggered when a child's dropdown option is selected. e.g.)
// In parent container
handleSelect(object) {
// Do logic here.
// e.g. if the first child selects an option by
if (object.selectedChild === 'FIRST')
const selected = object.selected;
// remove selected from an array
this.setState({ ... update the second dropdown selectables });
}
// In child component. Call this when option is selected
handleSelect(value) {
const obj = { selectedChild: 'FIRST' , selected: value };
this.props.onSelect(obj); // call parent function
}
3.When a child calls
this.props.onSelect(obj)
It will update the state in parent accordingly, which will pass down updated props to the children when it re-renders
Then child will get re-rendered with updated selectable object.
New contributor
New contributor
answered Nov 10 at 15:16
Junhouse
212
212
New contributor
New contributor
I am able to updated the state but change in state value doesnt reflecting on dropdown control
– Ravi Mathpal
2 days ago
In child component, you can use componentDidUpdate to see if the props have changed. If the props from a parent is different than the previous pros meaning the drop down selectables have changed then you can try to re-render at the child component level. Please try it and see if it works.
– Junhouse
2 days ago
add a comment |
I am able to updated the state but change in state value doesnt reflecting on dropdown control
– Ravi Mathpal
2 days ago
In child component, you can use componentDidUpdate to see if the props have changed. If the props from a parent is different than the previous pros meaning the drop down selectables have changed then you can try to re-render at the child component level. Please try it and see if it works.
– Junhouse
2 days ago
I am able to updated the state but change in state value doesnt reflecting on dropdown control
– Ravi Mathpal
2 days ago
I am able to updated the state but change in state value doesnt reflecting on dropdown control
– Ravi Mathpal
2 days ago
In child component, you can use componentDidUpdate to see if the props have changed. If the props from a parent is different than the previous pros meaning the drop down selectables have changed then you can try to re-render at the child component level. Please try it and see if it works.
– Junhouse
2 days ago
In child component, you can use componentDidUpdate to see if the props have changed. If the props from a parent is different than the previous pros meaning the drop down selectables have changed then you can try to re-render at the child component level. Please try it and see if it works.
– Junhouse
2 days ago
add a comment |
up vote
0
down vote
I can help you only with a state shape. Components handling should be your own work.
You can use a shared state for your dropdowns. Where you will to store an array of items and track selected item.
For example somewhere in a container:
state = {
data: [
{
name: 'Nokia',
selected: false,
},
{
name: 'Apple',
selected: false,
},
{
name: 'OnePlus',
selected: false,
}
]
}
Inside your dropdown component simple filter this array with .filter()
like this:
this.state.data.filter((item) => !item.selected)
Handle select logic and selected item for dropdown label should not be a problem.
add a comment |
up vote
0
down vote
I can help you only with a state shape. Components handling should be your own work.
You can use a shared state for your dropdowns. Where you will to store an array of items and track selected item.
For example somewhere in a container:
state = {
data: [
{
name: 'Nokia',
selected: false,
},
{
name: 'Apple',
selected: false,
},
{
name: 'OnePlus',
selected: false,
}
]
}
Inside your dropdown component simple filter this array with .filter()
like this:
this.state.data.filter((item) => !item.selected)
Handle select logic and selected item for dropdown label should not be a problem.
add a comment |
up vote
0
down vote
up vote
0
down vote
I can help you only with a state shape. Components handling should be your own work.
You can use a shared state for your dropdowns. Where you will to store an array of items and track selected item.
For example somewhere in a container:
state = {
data: [
{
name: 'Nokia',
selected: false,
},
{
name: 'Apple',
selected: false,
},
{
name: 'OnePlus',
selected: false,
}
]
}
Inside your dropdown component simple filter this array with .filter()
like this:
this.state.data.filter((item) => !item.selected)
Handle select logic and selected item for dropdown label should not be a problem.
I can help you only with a state shape. Components handling should be your own work.
You can use a shared state for your dropdowns. Where you will to store an array of items and track selected item.
For example somewhere in a container:
state = {
data: [
{
name: 'Nokia',
selected: false,
},
{
name: 'Apple',
selected: false,
},
{
name: 'OnePlus',
selected: false,
}
]
}
Inside your dropdown component simple filter this array with .filter()
like this:
this.state.data.filter((item) => !item.selected)
Handle select logic and selected item for dropdown label should not be a problem.
answered Nov 10 at 17:03
Kort
886
886
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240026%2freactjs-component-state-dropdown-not-updating%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Did you do a web search? You can't be the first with that kind of problem.
– Robert
Nov 11 at 2:29
I try but didn't find the solution
– Ravi Mathpal
2 days ago