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.




  1. Three instances of components get rendered initially.


  2. 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










share|improve this question
























  • 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















up vote
-1
down vote

favorite












I have a reactjs custom dropdown component which contains a list of mobiles.




  1. Three instances of components get rendered initially.


  2. 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










share|improve this question
























  • 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













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.




  1. Three instances of components get rendered initially.


  2. 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










share|improve this question















I have a reactjs custom dropdown component which contains a list of mobiles.




  1. Three instances of components get rendered initially.


  2. 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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






share|improve this answer








New contributor




Prerna Chuttani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    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.






    share|improve this answer








    New contributor




    Junhouse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • 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


















    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.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      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
































      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






      share|improve this answer








      New contributor




      Prerna Chuttani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















        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






        share|improve this answer








        New contributor




        Prerna Chuttani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















          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






          share|improve this answer








          New contributor




          Prerna Chuttani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          You can either keep the options of your dropdown in state and maintain a different state for options of each dropdown







          share|improve this answer








          New contributor




          Prerna Chuttani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Prerna Chuttani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 10 at 15:14









          Prerna Chuttani

          1




          1




          New contributor




          Prerna Chuttani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Prerna Chuttani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Prerna Chuttani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.
























              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.






              share|improve this answer








              New contributor




              Junhouse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.


















              • 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















              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.






              share|improve this answer








              New contributor




              Junhouse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.


















              • 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













              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.






              share|improve this answer








              New contributor




              Junhouse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              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.







              share|improve this answer








              New contributor




              Junhouse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              share|improve this answer



              share|improve this answer






              New contributor




              Junhouse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.









              answered Nov 10 at 15:16









              Junhouse

              212




              212




              New contributor




              Junhouse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





              New contributor





              Junhouse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              Junhouse is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.












              • 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










              • 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










              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.






              share|improve this answer

























                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.






                share|improve this answer























                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 17:03









                  Kort

                  886




                  886






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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




















































































                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine