簡體   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