【发布时间】: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