react 多个html,reactjs – 使用React-Router,每页有一个布局页面或多个组件

如果我正确理解你,实现你将在你的Route中定义多个组件。你可以使用它像(

example from the docs):

// think of it outside the context of the router,if you had pluggable

// portions of your `render`,you might do it like this

,sidebar: }}/>

// So with the router it looks like this:

const routes = (

)

class App extends React.Component {

render () {

const { main,sidebar } = this.props;

return (

{main}

{sidebar}

)

}

}

class Users extends React.Component {

render () {

return (

{/* if at "/users/123" `children` will be */}

{/* UseRSSidebar will also get as this.props.children,so its a little weird,but you can decide which one wants

to continue with the nesting */}

{this.props.children}

)

}

}

编辑:

根据@ Luiz的评论:

In the latest version of router the components are in the root of the props object

所以:

const { main,sidebar } = this.props.children;

变为:

const { main,sidebar } = this.props;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React 中,如果同一个组件多个页面使用,而这些页面地址的变化仅仅是参数的不同,React 不会重新渲染组件,因为它会认为这些组件都是相同的,因此页面不会刷新。 解决这个问题的一个方法是在组件内部监听地址参数的变化,然后根据参数的变化重新渲染组件。这可以通过在组件使用 `componentDidUpdate` 方法来实现。在这个方法中,你可以检查当前的地址参数和上一次的地址参数是否有变化,如果有变化就重新渲染组件。 例如,假设你的组件是 `MyComponent`,它使用了 `react-router-dom` 库中的 `withRouter` 方法来获取路由信息。在 `MyComponent` 组件中,你可以这样实现重新渲染: ```jsx import React, { Component } from 'react'; import { withRouter } from 'react-router-dom'; class MyComponent extends Component { componentDidUpdate(prevProps) { const { match: { params: { id } } } = this.props; const { match: { params: { id: prevId } } } = prevProps; // 如果地址参数变化了,就重新渲染组件 if (id !== prevId) { this.forceUpdate(); } } render() { const { match: { params: { id } } } = this.props; return ( <div> <p>当前地址参数为:{id}</p> </div> ); } } export default withRouter(MyComponent); ``` 在上面的代码中,我们在 `componentDidUpdate` 方法中获取当前的地址参数和上一次的地址参数,如果它们不相同,就调用 `forceUpdate` 方法强制重新渲染组件。 希望这个方法可以帮助你解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值