繁体   English   中英

在道具更改时反应重新渲染组件

[英]React Re-Render Component on props Change

我的 Tabbar 组件中有一个 Tabbar,我更改了其中的索引道具:

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;

然后在我的其他组件中,我想在道具更改后重新渲染片段。 这是我的代码:

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;

方法“handleRender”应该处理渲染。 我尝试使用“componentDidMount”或“componentDidUpdate”,但没有成功。

我怎样才能让它发挥作用?

先感谢您!

由于这个原因,您不需要在子组件中有状态您可以简单地在父组件中有一个回调并在子组件中调用它,如下所示。

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;

在父级中,您保持活动索引状态

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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