【问题标题】:React Re-Render Component on props ChangeReact Re-Render Component on props Change
【发布时间】:2021-02-01 08:17:14
【问题描述】:

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

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”,但没有成功。

我怎样才能让它工作?

提前谢谢你!

【问题讨论】:

  • 你在哪里使用标签栏?
  • @GuruparanGiritharan 在 render 方法中,我编辑了代码。
  • 你想改变 parent 的状态吗?喜欢活动标签
  • @GuruparanGiritharan 是的,然后,根据所选标签更改子项的显示。
  • 请检查我的答案

标签: reactjs components render


【解决方案1】:

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

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;

【讨论】:

  • 最好有一个如下所示的 handlerender 函数,您可以在其中根据活动选项卡找到项目并显示 handleRender = () => { const item = this.state.tabs.find((item) => {返回 item.index === this.state.activeIndex; });如果(项目)返回 {item.name};否则返回
    ; };
  • 感谢您的帮助,但您的回答也不起作用。
  • @hamedjvh 你试过了吗?
猜你喜欢
  • 1970-01-01
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 2019-04-22
  • 2020-07-13
  • 2021-10-18
相关资源
最近更新 更多