简体   繁体   English

React Router 4-组件重新安装而不是重新渲染

[英]React Router 4 - Component Remounting Instead of Re-Rendering

I'm setting up a React project that uses React Router 4 ("react-router-dom": "^4.3.1"). 我正在设置一个使用React Router 4的React项目(“ react-router-dom”:“ ^ 4.3.1”)。 I have a SideBar and a NavBar and then the main part of the app (content), which changes depending on the route. 我有一个SideBar和NavBar,然后是应用程序的主要部分(内容),该部分随路线而变化。 The issue I'm seeing is, when I link to a new Component from a Component within the content, the Component renders as I would expect, calling only that Component's componentDidMount() . 我看到的问题是,当我从内容中的组件链接到新组件时,该组件将按照我的期望进行渲染,仅调用该组件的componentDidMount() When the SideBar changes the route though, the entire App Component remounts, and its componentDidMount() gets called again. 但是,当补充工具栏更改路线时,将重新安装整个应用程序组件,并再次调用其componentDidMount()

The Root component creates the BrowserRouter 根组件创建BrowserRouter

<BrowserRouter>
   <Switch>
     <Route path="/login" component={Login}/>
     <Route path="/register" component={Register}/>
     <Route component={App}/>
   </Switch>
</BrowserRouter>

The App then creates the SideBar, NavBar, and content within the route 然后,应用程序会在路线中创建边栏,导航栏和内容

 render() {
    return (
      <div>
        <NavBar user={this.state.user} market={this.state.market}/>
        <SideBar user={this.state.user} market={this.state.market}/>
        <Route path="/market" render={props => <Market user={this.state.user} market={this.state.market} {...props}/>} exact={true} />
        <Route path="/user" render={props => <User user={this.state.user} market={this.state.market} {...props}/>} exact={true} />
      </div>
    );
  }

When a button on the Market page is pressed, it triggers the following code, which immediately renders the User page (as I would expect). 当按下“市场”页面上的按钮时,它将触发以下代码,该代码立即呈现“用户”页面(正如我期望的那样)。

<Link to={"/user"} className="btn>Link</Link>

When I link to this page from the SideBar though, with the following code, it causes App to remount, and calls componentDidMount() again. 但是,当我使用以下代码从SideBar链接到此页面时,它将导致App重新安装,并再次调用componentDidMount()

<Link to="/user">

I'm trying to avoid having App's componentDidMount() ever getting called after the initial call, because I set it up to store the logged in user, and other init() type stuff. 我试图避免在首次调用之后调用App的componentDidMount(),因为我将其设置为存储登录的用户以及其他init()类型的东西。 I would think the way I have it set up, this would happen, but apparently it isn't. 我想我会以这种方式设置它,但确实没有。 How should I change my Router set up to do this? 我应该如何更改路由器设置来做到这一点?

It turns out it wasns't anything React related. 事实证明,这与React没有任何关系。 I bought a theme, and the theme was capturing the mouse clicks and causing the page refresh. 我购买了一个主题,该主题是捕获鼠标单击并导致页面刷新。 When I took off the theme's events, the routing worked properly. 当我取消主题活动时,路由正常工作。

You have to place the side-bar && navbar component inside root componenet browser router tag. 您必须将侧栏&& navbar组件放置在root componenet浏览器路由器标签内。 So when a user logs in your app, the root component loads only once and side bar and navbar also once , so the other component you want to will be loaded using switch tag. 因此,当用户登录您的应用程序时,根组件仅加载一次,边栏和导航栏也加载一次,因此您要使用的其他组件将使用switch标签加载。 Example. 例。

<BrowserRouter>

            <div>
                <NavBar fetchChannelKey={this.fetchChannelKey} updateActive={this.updateActive} activePage={this.state.activePage} updateRoutes={this.handleUpdate}/>
                <SideBar updateActive={this.updateActive} activePage={this.state.activePage}/>
                <Switch>
                    <Route exact path="/" component={HomeIndex} />
                    <Route path="/home" component={HomeIndex} />
                    <Route path="/customer/:id" component={CustomerIndex} />
                    <Route path="/users/:id" component={UsersIndex} />
                    <switch/>
               <Browser/>
           <div/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM