简体   繁体   English

当父母的状态发生变化时,让孩子不反应

[英]Making react children NOT to refresh when parents state changes

So here is my problem. 所以这是我的问题。 I have an root component that contains navigation and Switch with every component in my page. 我有一个根组件,其中包含导航和“切换”页面中的每个组件。 Navigation is sliding in and out from the left and the way I'm doing this, I'm changing the state in root component, passing prop to and deciding whether or not, should I add class to my nav. 导航从左侧滑入和滑出,我这样做的方式是,更改根组件的状态,将prop传递给并确定是否应将类添加到导航中。 The problem is that every component in my app is re-rendering on opening/closing nav. 问题是我的应用程序中的每个组件都在打开/关闭导航时重新呈现。 Here is my root component: 这是我的根组件:

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      navOpen: false
    }
  }
  toggleNav = () => {
    this.setState({
      navOpen: !this.state.navOpen
    })
  }
  closeNav = (e) => {
    if (this.state.navOpen) {
      this.setState({navOpen: false})
    }
  }

  render() {
    return (
      <main>
        <Header/>
        <Hamburger navOpen={this.state.navOpen} toggleNav={this.toggleNav}/>
        <Navigation navOpen={this.state.navOpen} toggleNav={this.toggleNav}/>
        <section className="container-fluid content" onClick={this.closeNav}>
          <Switch>
            <Route path="/add-recipe/:groupId?" component={NewRecipe}/>
            <Route path="/recipes/:page?/:sortType?/:size?" component={RecipeList}/>
            <Route path="/recipe/:id" component={Recipe}/>
            <Route path="/sign/" component={SignForm}/>
            <Route path="/user/:id" component={User}/>
          </Switch>
        </section>
      </main>

    );
  }
  componentDidMount() {
    this.props.userActions.getUser(this.props.url);
  }
}

function mapStateToProps(state) {
  return {url: state.url.url, user: state.user.loggedUser}
}

function mapDispatchToProps(dispatch) {
  return {
    userActions: bindActionCreators(userActions, dispatch)
  }
}

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(App));

Navigation is the only component ( besides hamburger) that cares about his parents state so I have no idea why everything is re-rendering. 导航是唯一关心他父母状态的组件(除了汉堡包),所以我不知道为什么所有东西都要重新渲染。 Does anyone have some ideas? 有人有想法吗?

EDIT: 编辑:

I've added sCU to my nested components like that: 我已经将sCU添加到了这样的嵌套组件中:

shouldComponentUpdate(nextProps, nextState) {
    // console.log(this.props)
    // console.log("next")
    // console.log(nextProps)
    if (this.props == nextProps) {
      return false
    }
    return true
  }

But it didn't change anything. 但这并没有改变任何东西。 When I open the navigation props for routes remain the same but they still rerender. 当我打开导航时,路线的导航道具保持不变,但仍会重新显示。 I tried to move "navOpen" state to navigation and open it from root component via "ref" but every time I call its method I get "Cannot read property 'toggleNav' of null" 我试图将“ navOpen”状态移至导航并通过“ ref”从根组件中将其打开,但是每次调用其方法时,都会收到“无法读取null的'toggleNav'属性”

You can prevent re-rendering by implementing shouldComponentUpdate on the affected component. 您可以通过在受影响的组件上实现shouldComponentUpdate来防止重新渲染。 Beware though, that rendering does not mean that the DOM is being changed. 但是请注意,渲染并不意味着DOM被更改。 Rendering in react means that react creates a new copy of its internal node tree and compares it to the DOM to see if and where it needs to update it. 在react中进行渲染意味着react会创建其内部节点树的新副本,并将其与DOM进行比较,以查看是否需要更新它以及在何处对其进行更新。 So don't worry about re-renders unless your component is expensive to create (ie it performs a request to a backend or is heavily animated) 因此,除非您的组件创建成本很高(即,它向后端执行请求或动画效果很强),否则不要担心重新渲染

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

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