【问题标题】:Why is React component rerendering when props has not changed?为什么在 props 没有改变的情况下 React 组件会重新渲染?
【发布时间】:2020-08-01 17:51:27
【问题描述】:

我在 ReactJS 16.8.5 和 React-Redux 3.7.2 上构建了一个应用程序。当应用加载应用装载时,会设置初始存储并针对 Firebase 实时数据库设置数据库订阅。该应用程序包含侧边栏、标题和内容部分。通过使用 React Developer Tools 分析应用程序,我可以看到 Sidebar 被渲染了多次 - 触发了子组件的重新渲染。我已经实现了React.memo 以避免在道具更改时重新渲染。 据我所知,道具没有改变,但Sidebar 仍然重新渲染,这让我感到困惑。

app.js

//Imports etc...
const jsx = (
  <React.StrictMode>
    <Provider store={store}>
      <AppRouter />
    </Provider>
  </React.StrictMode>
)

let hasRendered = false
const renderApp = () => {
  if (!hasRendered) { //make sure app only renders one time
    ReactDOM.render(jsx, document.getElementById('app'))
    hasRendered = true
  }
}

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // Set initial store and db subscriptions
    renderApp()
  }
})

AppRouter.js

//Imports etc...
const AppRouter = ({}) => {
  //...
  return (
    <React.Fragment>
      //uses Router instead of BrowserRouter to use our own history and not the built in one
      <Router history={history}>    
        <div className="myApp">
          <Route path="">
            <Sidebar />
          </Route>
          //More routes here...
        </div>
      </Router>
    </React.Fragment>
  )
}
//...
export default connect(mapStateToProps, mapDispatchToProps)(AppRouter)

Sidebar.js

//Imports etc...
export const Sidebar = (props) => {
  const onRender = (id, phase, actualDuration, baseDuration, startTime, commitTime) => {
    if (id !== 'Sidebar') { return }
    console.log('Profile', phase, actualDuration)
  }
  return (
    <Profiler id="Sidebar" onRender={onRender}>
      <React.Fragment>
        {/* Contents of Sidebar */}
      </React.Fragment>
    </Profiler>
}

const mapStateToProps = (state) => {
  console.log('Sidebar mapStateToProps')
  return {
    //...
  }
}
const areEqual = (prevProps, nextProps) => {
  const areStatesEqual = _.isEqual(prevProps, nextProps)
  console.log('Profile Sidebar isEqual', areStatesEqual)
  return areStatesEqual
}
export default React.memo(connect(mapStateToProps, mapDispatchToProps)(Sidebar),areEqual)

Console output

Sidebar mapStateToProps 2 
Profile Sidebar mount 225 
Sidebar mapStateToProps 
Profile Sidebar isEqual true 
Sidebar mapStateToProps 
Profile Sidebar update 123 
Sidebar mapStateToProps 2 
Profile Sidebar update 21 
Sidebar mapStateToProps 
Profile Sidebar update 126 
Sidebar mapStateToProps 
Profile Sidebar update 166 
Sidebar mapStateToProps 
Profile Sidebar update 99 
Sidebar mapStateToProps 
Sidebar mapStateToProps 
Sidebar mapStateToProps 
Sidebar mapStateToProps
Sidebar mapStateToProps 
Sidebar mapStateToProps 
Profile Sidebar update 110 
Sidebar mapStateToProps 
Sidebar mapStateToProps 
Sidebar mapStateToProps 
Profile Sidebar update 4

为什么 Sidebar 在 props 没有变化的情况下会重新渲染 八次?预计会重新渲染一次?

亲切的问候 /K

【问题讨论】:

  • 您的React.memo 的第二个参数返回相反的值。它应该返回重新渲染是否会产生相同的结果
  • 感谢@GalAbra 指出逻辑错误!在更改 React.memo 函数的返回值后,我已经用结果更新了上面的代码示例! /K
  • 它没有解决问题吗? :(
  • 不,抱歉! :( 结果完全一样。这让我有点困惑。是的 - 我已经仔细检查了我的代码。;) /K
  • 在 mapStateToProps 中放置一个控制台日志,我怀疑有一个状态变化导致 mapStateToProps 返回一个新的引用,因为你没有记住它(比如使用重新选择)。因此,如果 redux 状态中的任何内容发生更改并且您拥有const mapStateToProps=state=&gt;({new:'reference'}),它将导致连接的组件重新渲染。

标签: reactjs redux rerender


【解决方案1】:

如评论;当 mapStateToProps 返回一个新对象时,即使相关值没有变化,它也会重新渲染连接的组件。

这是因为{} !== {},具有相同属性和值的对象不等于具有相同属性和值的另一个对象,因为 React 比较对象引用而不是对象的值。这就是为什么你不能通过改变它来改变状态。变异会更改对象中的值,但不会更改对对象的引用。

您的 mapStateToProps 必须在第 2 层返回一个新引用,以便它使用相同的值重新渲染,因此 {val:1} 不会重新渲染,但 {something:{val:1}} 会。

下面的代码展示了不记忆 mapStateToProps 的结果如何导致重新渲染:

const { Provider, connect, useDispatch } = ReactRedux;
const { createStore } = Redux;
const { createSelector } = Reselect;
const { useRef, useEffect, memo } = React;

const state = { val: 1 };
//returning a new state every action but no values
//  have been changed
const reducer = () => ({ ...state });
const store = createStore(
  reducer,
  { ...state },
  window.__REDUX_DEVTOOLS_EXTENSION__ &&
    window.__REDUX_DEVTOOLS_EXTENSION__()
);
const Component = (props) => {
  const rendered = useRef(0);
  rendered.current++;
  return (
    <div>
      <div>rendered:{rendered.current} times</div>
      props:<pre>{JSON.stringify(props)}</pre>
    </div>
  );
};
const selectVal = (state) => state.val;
const selectMapStateToProps = createSelector(
  selectVal,
  //will only re create this object when val changes
  (val) => console.log('val changed') || { mem: { val } }
);
const memoizedMapStateToProps = selectMapStateToProps;
const mapStateToProps = ({ val }) =>
  ({ nonMem: { val } }); //re creates props.nonMem every time
const MemoizedConnected = connect(memoizedMapStateToProps)(
  Component
);
//this mapStateToProps will create a props of {val:1}
//  pure components (returned by connect) will compare each property
//  of the prop object and not the props as a whole. Since props.val
//  never changed between renders it won't re render
const OneLevelConnect = connect(({ val }) => ({ val }))(
  Component
);
const Connected = connect(mapStateToProps)(Component);
const Pure = memo(function Pure() {
  //props never change so this will only be rendered once
  console.log('props never change so wont re render Pure');
  return (
    <div>
      <Connected />
      <MemoizedConnected />
      <OneLevelConnect />
    </div>
  );
});
const App = () => {
  const dispatch = useDispatch();
  useEffect(
    //dispatch an action every second, this will create a new
    // state ref but state.val never changes
    () => {
      setInterval(() => dispatch({ type: 88 }), 1000);
    },
    [dispatch] //dispatch never changes but linting tools don't know that
  );
  return <Pure />;
};

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.5/redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/7.2.0/react-redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reselect/4.0.0/reselect.min.js"></script>


<div id="root"></div>

mapStateToProps 函数还可以通过传递返回函数的函数进行更多优化。这样,您可以在组件挂载时创建一个记忆选择器。这可以在列表项中使用(参见下面的代码)。

const { useRef, useEffect } = React;
const {
  Provider,
  useDispatch,
  useSelector,
  connect,
} = ReactRedux;
const { createStore } = Redux;
const { createSelector } = Reselect;
const state = {
  data: [
    {
      id: 1,
      firstName: 'Ben',
      lastName: 'Token',
    },
    {
      id: 2,
      firstName: 'Susan',
      lastName: 'Smith',
    },
  ],
};
//returning a new state every action but no values
//  have been changed
const reducer = () => ({ ...state });
const store = createStore(
  reducer,
  { ...state },
  window.__REDUX_DEVTOOLS_EXTENSION__ &&
    window.__REDUX_DEVTOOLS_EXTENSION__()
);
//selectors
const selectData = (state) => state.data;
const selectPerson = createSelector(
  selectData,
  (_, id) => id, //pass second argument to select person by id
  (people, _id) => people.find(({ id }) => id === _id)
);
//function that will create props for person component
//  from person out of state
const asPersonProps = (person) => ({
  person: {
    fullName: person.firstName + ' ' + person.lastName,
  },
});
//in ConnectedPerson all components share this selector
const selectPersonProps = createSelector(
  (state, { id }) => selectPerson(state, id),
  asPersonProps
);
//in OptimizedConnectedPerson each component has it's own
//  selector
const createSelectPersonProps = () =>
  createSelector(
    (state, { id }) => selectPerson(state, id),
    asPersonProps
  );

const Person = (props) => {
  const rendered = useRef(0);
  rendered.current++;
  return (
    <li>
      <div>rendered:{rendered.current} times</div>
      props:<pre>{JSON.stringify(props)}</pre>
    </li>
  );
};
//optimized mapStateToProps
const mapPersonStateToProps = createSelectPersonProps;
const OptimizedConnectedPerson = connect(
  mapPersonStateToProps
)(Person);
const ConnectedPerson = connect(selectPersonProps)(Person);
const App = () => {
  const dispatch = useDispatch();
  const people = useSelector(selectData);
  const rendered = useRef(0);
  rendered.current++;
  useEffect(
    //dispatch an action every second, this will create a new
    // state ref but state.val never changes
    () => {
      setInterval(() => dispatch({ type: 88 }), 1000);
    },
    [dispatch] //dispatch never changes but linting tools don't know that
  );
  return (
    <div>
      <h2>app rendered {rendered.current} times</h2>
      <h3>Connected person (will re render)</h3>
      <ul>
        {people.map(({ id }) => (
          <ConnectedPerson key={id} id={id} />
        ))}
      </ul>
      <h3>
        Optimized Connected person (will not re render)
      </h3>
      <ul>
        {people.map(({ id }) => (
          <OptimizedConnectedPerson key={id} id={id} />
        ))}
      </ul>
    </div>
  );
};

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.5/redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/7.2.0/react-redux.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/reselect/4.0.0/reselect.min.js"></script>


<div id="root"></div>

【讨论】:

  • 哇!非常感谢@HMR!将尝试 createSelector 来记忆 mapStateToProps 的结果!! /K
  • 感谢@HMR 的精彩回答!我一直在阅读和摆弄 Reselect - 并且取得了一些成功。但是我的组件仍然比预期的要重新渲染更多。我肯定错过了什么?我在这里发布了一个新问题stackoverflow.com/questions/61405482/…亲切的问候/K
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-27
  • 2018-03-26
  • 2020-08-18
  • 1970-01-01
  • 2020-09-05
相关资源
最近更新 更多