Issues with rendering the data in the state of react component

yetunde okunola

enter image description hereI am trying to setState of two different sate(options1 ad options2) in my component with data gotten from firebase. The data in the state of the component is to be used for a dependent drop-down. I have been able to setState with the new firebase data(I know this because I see that the state of my component is the data from my database using the react developer tools). However, the data is not being rendered on the dropdown. Is there something I am doing wrong or should be doing.

class Impairement extends Component {
      constructor() {
        super();
        this.state = {
          name: "React",
          selectedOption: {},
          selectedOption2: {},
          options1: [],
          options2: []
        };
      }

      componentDidMount() {
        this.getnewData();
      }
      //firebase fetch
      getnewData() {
        var rootRef = firebase
          .database()
          .ref()
          .child("test");
        var opt = [];
        rootRef.on("child_added", snapshot => {
          opt.push({
            value: snapshot.val().value,
            label: snapshot.val().label
          });
        });

        var rootRef = firebase
          .database()
          .ref()
          .child("test2");
        var optio = [];
        rootRef.on("child_added", snapshot => {
          optio.push({
            label: snapshot.val().label,
            link: snapshot.val().link,
            value: snapshot.val().value
          });
        });

        this.setState({
          options1: opt
        });
        this.setState({
          options2: optio
        });
      }

      handleChange1 = selectedOption => {
        this.setState({ selectedOption });
      };

      handleChange2 = selectedOption => {
        this.setState({ selectedOption2: selectedOption });
      };

      render() {
        const filteredOptions = this.state.options2.filter(
          o => o.link === this.state.selectedOption.value
        );

        return (
          <div>
            <p>Select Domain</p>
            <Select
              name="form-field-name"
              value={this.state.selectedOption.value}
              onChange={this.handleChange1}
              options={this.state.options1}
            />
            <p>Then Subdomain</p>
            <Select
              name="form-field-name"
              value={this.state.selectedOption2.value}
              onChange={this.handleChange2}
              options={filteredOptions}
            />
          </div>
        );
      }
    }

    export default Impairement;
Gonzalo.-

You have to setState inside your event function

currently you're not doing it, those events are asynchronous so when you do setState now, the arrays are empty

rootRef.on("child_added", snapshot => {
     let element = {
            label: snapshot.val().label,
            link: snapshot.val().link,
            value: snapshot.val().value
          };    
    this.setState(prevState => ({ options1: [...prevState.options1, element]  })
});
// notice I store the second reference in another variable
rootRef2.on("child_added", snapshot => {
          let element = {
            label: snapshot.val().label,
            link: snapshot.val().link,
            value: snapshot.val().value
          });
       this.setState(prevState => ({ options2: [...prevState.options2, element]  })
});

furthermore, I think you can rewrite the code a bit because you're using the same variable with two different instances of the same event. But this at least should solve your initial problem

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Issues with text rendering in react component

React component not rendering on state update

Data are not rendering in react component

React: Child component not rendering on parents state change

React component not re-rendering on state change

Conditional rendering in React based on current component state

React function component state not re-rendering?

React component is rendering but not updating when state is updating

React: dynamic rendering of a component based on state

React state updating but not rendering on child component

React how to pass state without rendering component

Rendering React component from Redux state property

React State Issues with Classful to Functional Component Migration

React component not re-rendering on component state change

Confused why react component not rendering from data

React component elements/data not rendering but logs in console

API data rendering Issue in Child component in React

Rendering component with its state

React-redux component is not re-rendering after state change

Why is React rendering my component over and over with an unchanged state?

React component not re-rendering when state changes

React component rendering even when there is no change in the state value

Is the react keep the old state in the component when rendering web page?

React re-rendering resets component's hover state

React-Native-Component not rendering when state is changed

How to stop react re-rendering component, if part of the state changes?

React component that takes state variable as a parameter not rendering on update

how to update react state without re rendering component?

React Child Component not re-rendering mapped state

TOP Ranking

HotTag

Archive