我有主布局组件,通常当我更改路线链接 {this.props.child} 时只需要更改。
这是我的布局组件。index.js
<React.Fragment>
{this.props.settings.dark_overlay ? <div className="dark"></div> : ''}
<div className="site">
<Header />
<div className="sitecontent">
<div className="container flex-or">
<Content>
{this.props.children}
</Content>
{this.props.AsideType === "settings" ? null : <React.Fragment><AsideFirst /><AsideSecond /></React.Fragment>}
</div>
</div>
<Footer />
</div>
</React.Fragment>
当我构建我的应用程序时,首先从服务器获取数据,看起来像这样;
AsideFirst 组件
constructor(props) {
super(props);
this.state = {
trends: []
}
this.handleButons = this.handleButons.bind(this)
}
handleButons(e){
let type = e.target.name
if (type === "refresh") {
this.getTrend()
}
}
componentDidMount() {
this.getTrend()
}
getTrend() {
axios.post('/api/data/title/trend')
.then(res => {
const trends = res.data;
console.log(trends)
this.setState({ trends: trends });
})
}
在这里没问题...
但,
当我更改路由组件重新创建时,这是我的app.js*
<React.Fragment>
<ReactNotification />
<Router history={history}>
<Switch>
<Route path="/" exact component={Index} />
<Route path="/kayit/" exact component={Register} />
<Route path="/giris/" exact component={Login} />
<Route path="/aktivasyon" exact component={Activation} />
<Route path="/hesap/ayarlar" exact component={AccountSettings} />
<Route path="/cikis" exact component={Logout} />
<Route path="/:string/" exact component={ComplexPush} />
<Route component={Page404} />
</Switch>
</Router>
</React.Fragment>
我的布局组件(所以 AsideFirst)在每次路由更改时都从服务器获取数据,尽管路由组件是子组件。示例路由组件;
复杂推送组件
<Layout>
<div className="complex-header">
<Link to="/"><h4>{str}</h4></Link>
{!id ? <NoEntry /> : "" }
</div>
<Entry />
<Button variant="primary">Gönder</Button>
</Layout>
因此,尽管我的所有父组件布局,但当路线更改时,Asidefirst(内部布局)一直在安装。我不想要这个。我该如何解决这个问题?