How to optimize small updates to props of nested component in React + Redux?

前端 未结 1 1008
忘掉有多难
忘掉有多难 2021-01-29 18:14

Example code: https://github.com/d6u/example-redux-update-nested-props/blob/master/one-connect/index.js

View live demo: http://d6u.github.io/example-redux-update-nested-

1条回答
  •  被撕碎了的回忆
    2021-01-29 18:49

    I’m not sure where const App = connect((state) => state)(RepoList) comes from.
    The corresponding example in React Redux docs has a notice:

    Don’t do this! It kills any performance optimizations because TodoApp will rerender after every action. It’s better to have more granular connect() on several components in your view hierarchy that each only listen to a relevant slice of the state.

    We don’t suggest using this pattern. Rather, each connect specifically so it reads its own data in its mapStateToProps. The “tree-view” example shows how to do it.

    If you make the state shape more normalized (right now it’s all nested), you can separate repoIds from reposById, and then only have your RepoList re-render if repoIds change. This way changes to individual repos won’t affect the list itself, and only the corresponding Repo will get re-rendered. This pull request might give you an idea of how that could work. The “real-world” example shows how you can write reducers that deal with normalized data.

    Note that in order to really benefit from the performance offered by normalizing the tree you need to do exactly like this pull request does and pass a mapStateToProps() factory to connect():

    const makeMapStateToProps = (initialState, initialOwnProps) => {
      const { id } = initialOwnProps
      const mapStateToProps = (state) => {
        const { todos } = state
        const todo = todos.byId[id]
        return {
          todo
        }
      }
      return mapStateToProps
    }
    
    export default connect(
      makeMapStateToProps
    )(TodoItem)
    

    The reason this is important is because we know IDs never change. Using ownProps comes with a performance penalty: the inner props have to be recalculate any time the outer props change. However using initialOwnProps does not incur this penalty because it is only used once.

    A fast version of your example would look like this:

    import React from 'react';
    import ReactDOM from 'react-dom';
    import {createStore} from 'redux';
    import {Provider, connect} from 'react-redux';
    import set from 'lodash/fp/set';
    import pipe from 'lodash/fp/pipe';
    import groupBy from 'lodash/fp/groupBy';
    import mapValues from 'lodash/fp/mapValues';
    
    const UPDATE_TAG = 'UPDATE_TAG';
    
    const reposById = pipe(
      groupBy('id'),
      mapValues(repos => repos[0])
    )(require('json!../repos.json'));
    
    const repoIds = Object.keys(reposById);
    
    const store = createStore((state = {repoIds, reposById}, action) => {
      switch (action.type) {
      case UPDATE_TAG:
        return set('reposById.1.tags[0]', {id: 213, text: 'Node.js'}, state);
      default:
        return state;
      }
    });
    
    const Repo  = ({repo}) => {
      const [authorName, repoName] = repo.full_name.split('/');
      return (
        
  • {repoName} / {authorName}
      {repo.tags.map((tag) =>
    1. {tag.text}
    2. )}
    {repo.description}
  • ); } const ConnectedRepo = connect( (initialState, initialOwnProps) => (state) => ({ repo: state.reposById[initialOwnProps.repoId] }) )(Repo); const RepoList = ({repoIds}) => { return
      {repoIds.map((id) => )}
    ; }; const App = connect( (state) => ({repoIds: state.repoIds}) )(RepoList); console.time('INITIAL'); ReactDOM.render( , document.getElementById('app') ); console.timeEnd('INITIAL'); setTimeout(() => { console.time('DISPATCH'); store.dispatch({ type: UPDATE_TAG }); console.timeEnd('DISPATCH'); }, 1000);

    Note that I changed connect() in ConnectedRepo to use a factory with initialOwnProps rather than ownProps. This lets React Redux skip all the prop re-evaluation.

    I also removed the unnecessary shouldComponentUpdate() on the because React Redux takes care of implementing it in connect().

    This approach beats both previous approaches in my testing:

    one-connect.js: 43.272ms
    repo-connect.js before changes: 61.781ms
    repo-connect.js after changes: 19.954ms
    

    Finally, if you need to display such a ton of data, it can’t fit in the screen anyway. In this case a better solution is to use a virtualized table so you can render thousands of rows without the performance overhead of actually displaying them.


    I got a solution by replacing every tags with an observable inside reducer.

    If it has side effects, it’s not a Redux reducer. It may work, but I suggest to put code like this outside Redux to avoid confusion. Redux reducers must be pure functions, and they may not call onNext on subjects.

    0 讨论(0)
提交回复
热议问题