简体   繁体   中英

React Re-Render Component on props Change

I have a Tabbar in my Tabbar Component, Which I Change the index props in it :

class Tabbar extends Component {

state = {
    index: this.props.index,
    name: this.props.name,
    image: this.props.image
};

changeTabs = () => {
    this.setState({index: this.props.index});
}

render() {
    return (
        <React.Fragment>    
        <div id={this.state.index} className="col">
            <button onClick={this.changeTabs}></button>
        </div>
        </React.Fragment>
        
    );

}

}

export default Tabbar;

And Then In my Other Component, I Wanna Re-Render a fragment after props change. Here's my Code :

import Tabbar from './Tabbar';

class Tabview extends Component {

    constructor(props) {
        super(props);
        this.state = {
            tabs: [
                {index: 0, name: "tab0", image:require('../Assets/profile.svg'),childView: {ProfilePage} },
                {index: 1, name: "tab1", image:require('../Assets/home.svg'),childView: {HomePage}},
                {index: 2, name: "tab2", image:require('../Assets/blog.svg'),childView: {BlogPage}},
              ],
        }
    }

    handleRender = () => {
        this.state.tabs.map(item => {
            if (item.index === this.props.index) {
                return <item.childView/>;
            }
        })
        return <BlogPage/>;
    }


    render() {

        return (
        <div>
            <Header/> 

            {this.handleRender()}
            {this.state.tabs.map(item => 
                        <Tabbar key={item.index} index={item.index} name={item.name} image={item.image}/>
                    )}


    
        </div>
            

        );

    }

}

export default Tabview;

The Method "handleRender" should handle the rendering. I tried to use "componentDidMount" or "componentDidUpdate", But I didn't work.

How Can I Make it Work?

Thank you in advance!

You dont need to have a state in the child component for this reason You can simply have a callback in parent and call it in child component like below.

import React, { Component } from "react";

class Tabbar extends Component {
  render() {
    return (
      <React.Fragment>
        <div id={this.props.index} className="col">
          <button
            onClick={() => this.props.changeTabs(this.props.index)}
          ></button>
        </div>
      </React.Fragment>
    );
  }
}

export default Tabbar;

And in parent you maintain the active index state

import Tabbar from "./Tabbar";
import React, { Component } from "react";

class Tabview extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tabs: [
//your tabs
      ],
      activeIndex: 0
    };
  }

  handleRender = () => {
    this.state.tabs.map((item) => {
      if (item.index === this.state.activeIndex) {
        return <item.childView />;
      }
    });
    return <div />;
  };

  render() {
    return (
      <div>
        {this.handleRender()}
        {this.state.tabs.map((item) => (
          <Tabbar
            key={item.index}
            index={item.index}
            name={item.name}
            image={item.image}
            changeTabs={(index) => this.setState({ activeIndex: index })}
          />
        ))}
      </div>
    );
  }
}

export default Tabview;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM