React: dynamic rendering of a component based on state

Ruham

I have a Parent(blue box with three buttons) and a Child(red box that shows content) component, that render some text based on the Child's state. Basically, the rendered view is this:

enter image description here

The Child component:

class Child extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tab: this.props.tab
    }
  }
  render() {
    let text;

    if (this.props.tab === '1') {
      text = <div>text 1 </div>
    } else if (this.props.tab === '2') { 
      text = <div>text 2 </div>
    } else if (this.props.tab === '3') {
      text = <div>text 3 </div>
    }
  return (
    <div>
      {text}
    </div>
    );
  } 
}

export default Child;

The Parent component:

class Parent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tab: null
    }
    this.onOneClik = this.onOneClik.bind(this);
    this.onTwoClik = this.onTwoClik.bind(this);
    this.onThreeClick = this.onThreeClick.bind(this);
  }
  onOneClik(e) {
    this.setState({ tab: '1' });
  }
  onTwoClik(e) {
    this.setState({ tab: '2' });
  }
  onThreeClick(e) {
    this.setState({ tab: '3' });
  }
  render() {
    return (
      <div>
        <button type="button" onClick={this.onOneClik}>1</button>
        <button type="button" onClick={this.onTwoClik}>2</button>
        <button type="button" onClick={this.onThreeClik}>3</button>
      </div>
      <div>
        <Child tab={this.state.tab} />
      </div>
    );
  }
}

The issue is that I need to re-render the Child when the button is clicked, but the Child shows only the content of the button which was clicked first, even though the state of the Parent updates itself when different buttons are clicked.

What would be the way to dynamically render the Child without involving Links? I guess redux could help, but I'm not sure on how to wrap redux around this.

Tholle

A component will be re-rendered when its state or props changes. Instead of storing the initial tab prop in the state of the Child component, you can use the prop directly instead (you also have a few typos that are fixed below).

Example

class Child extends React.Component {
  render() {
    let text;

    if (this.props.tab === "1") {
      text = <div> text 1 </div>;
    } else if (this.props.tab === "2") {
      text = <div> text 2 </div>;
    } else if (this.props.tab === "3") {
      text = <div> text 3 </div>;
    }

    return <div>{text}</div>;
  }
}

class Parent extends React.Component {
  state = {
    tab: null
  };

  onOneClick = e => {
    this.setState({ tab: "1" });
  };
  onTwoClick = e => {
    this.setState({ tab: "2" });
  };
  onThreeClick = e => {
    this.setState({ tab: "3" });
  };

  render() {
    return (
      <div>
        <button type="button" onClick={this.onOneClick}>
          1
        </button>
        <button type="button" onClick={this.onTwoClick}>
          2
        </button>
        <button type="button" onClick={this.onThreeClick}>
          3
        </button>
        <Child tab={this.state.tab} />
      </div>
    );
  }
}


ReactDOM.render(<Parent />, document.getElementById('root'));
<script src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script>

<div id="root"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Conditional rendering in React based on current component state

React native: rendering conditional component based on state value change in Modal

React component not rendering on state update

Rendering a dynamic child component in React

React: Child component not rendering on parents state change

React component not re-rendering on state change

React function component state not re-rendering?

React component is rendering but not updating when state is updating

React state updating but not rendering on child component

React how to pass state without rendering component

Rendering React component from Redux state property

Issues with rendering the data in the state of react component

React Native conditional rendering based on token without storage inside component state

Basic React Route with dynamic ID not rendering component

Conditional rendering of a div based on state in react

Rendering Content Conditionally in React JS Based on the state

Conditionally rendering component based on routing in react

React component not re-rendering on component state change

React: DOM not reflecting component state in dynamic form

Conditional rendering of Youtube component based on redux state (cookie acceptance)

Material-ui component is not rendering in react class based component?

Rendering component with its state

Disabling button based on child component state in React

Show/Hide React Component based on State

Create dynamic React Route components based on state

TypeScript Dynamic Types based on React 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