繁体   English   中英

使用钩子响应上下文可防止重新渲染

[英]React context with hooks prevent re render

我使用带有钩子的 React 上下文作为我的 React 应用程序的状态管理器。 每次 store 中的值发生变化时,所有组件都会重新渲染。

有没有办法阻止 React 组件重新渲染?

店铺配置:

import React, { useReducer } from "react";
import rootReducer from "./reducers/rootReducer";

export const ApiContext = React.createContext();

export const Provider = ({ children }) => {
  const [state, dispatch] = useReducer(rootReducer, {});

  return (
    <ApiContext.Provider value={{ ...state, dispatch }}>
      {children}
    </ApiContext.Provider>
  );
};

减速器的一个例子:

import * as types from "./../actionTypes";

const initialState = {
  fetchedBooks: null
};

const bookReducer = (state = initialState, action) => {
  switch (action.type) {
    case types.GET_BOOKS:
      return { ...state, fetchedBooks: action.payload };

    default:
      return state;
  }
};

export default bookReducer;

Root reducer,可以组合尽可能多的reducer:

import userReducer from "./userReducer";
import bookReducer from "./bookReducer";

const rootReducer = ({ users, books }, action) => ({
  users: userReducer(users, action),
  books: bookReducer(books, action)
});

一个动作的例子:

import * as types from "../actionTypes";

export const getBooks = async dispatch => {
  const response = await fetch("https://jsonplaceholder.typicode.com/todos/1", {
    method: "GET"
  });

  const payload = await response.json();

  dispatch({
    type: types.GET_BOOKS,
    payload
  });
};
export default rootReducer;

这是书籍组件:

import React, { useContext, useEffect } from "react";
import { ApiContext } from "../../store/StoreProvider";
import { getBooks } from "../../store/actions/bookActions";

const Books = () => {
  const { dispatch, books } = useContext(ApiContext);
  const contextValue = useContext(ApiContext);

  useEffect(() => {
    setTimeout(() => {
      getBooks(dispatch);
    }, 1000);
  }, [dispatch]);

  console.log(contextValue);

  return (
    <ApiContext.Consumer>
      {value =>
        value.books ? (
          <div>
            {value.books &&
              value.books.fetchedBooks &&
              value.books.fetchedBooks.title}
          </div>
        ) : (
          <div>Loading...</div>
        )
      }
    </ApiContext.Consumer>
  );
};

export default Books;

当 Books 组件中的值发生变化时,另一个 my 组件 Users 重新渲染:

import React, { useContext, useEffect } from "react";
import { ApiContext } from "../../store/StoreProvider";
import { getUsers } from "../../store/actions/userActions";

const Users = () => {
  const { dispatch, users } = useContext(ApiContext);
  const contextValue = useContext(ApiContext);

  useEffect(() => {
    getUsers(true, dispatch);
  }, [dispatch]);

  console.log(contextValue, "Value from store");

  return <div>Users</div>;
};

export default Users;

优化上下文重新渲染的最佳方法是什么? 提前致谢!

BooksUsers当前在每个周期重新渲染 - 不仅是在商店价值发生变化的情况下。

1. prop 和 state 的变化

React 重新渲染整个子组件树,以组件为根,其中 props 或 state 发生了变化。 您通过getUsers更改父状态,因此BooksUsers重新呈现。

 const App = () => { const [state, dispatch] = React.useReducer( state => ({ count: state.count + 1 }), { count: 0 } ); return ( <div> <Child /> <button onClick={dispatch}>Increment</button> <p> Click the button! Child will be re-rendered on every state change, while not receiving any props (see console.log). </p> </div> ); } const Child = () => { console.log("render Child"); return "Hello Child "; }; ReactDOM.render(<App />, document.getElementById("root"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.0/umd/react.production.min.js" integrity="sha256-32Gmw5rBDXyMjg/73FgpukoTZdMrxuYW7tj8adbN8z4=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.0/umd/react-dom.production.min.js" integrity="sha256-bjQ42ac3EN0GqK40pC9gGi/YixvKyZ24qMP/9HiGW7w=" crossorigin="anonymous"></script> <div id="root"></div>

优化技术

使用React.memo来防止重新渲染合成,如果它自己的道具实际上没有改变。

// prevents Child re-render, when the button in above snippet is clicked
const Child = React.memo(() => {
  return "Hello Child ";
});
// equivalent to `PureComponent` or custom `shouldComponentUpdate` of class comps

重要提示: React.memo仅检查道具更改( useContext值更改触发重新渲染)!


2. 上下文变化

当上下文值更改时,所有上下文使用者 ( useContext ) 都会自动重新呈现。

// here object reference is always a new object literal = re-render every cycle
<ApiContext.Provider value={{ ...state, dispatch }}>
  {children}
</ApiContext.Provider>

优化技术

确保上下文值具有稳定的对象引用,例如通过useMemo Hook。

const [state, dispatch] = useReducer(rootReducer, {});
const store = React.useMemo(() => ({ state, dispatch }), [state])

<ApiContext.Provider value={store}>
  {children}
</ApiContext.Provider>

其他

不确定,为什么您将所有这些结构放在Books ,只需使用一个useContext

const { dispatch, books } = useContext(ApiContext);
// drop these
const contextValue = useContext(ApiContext); 
<ApiContext.Consumer> /* ... */ </ApiContext.Consumer>; 

您还可以使用React.memouseContext查看此代码示例

我相信这里发生的事情是预期的行为。 它呈现两次的原因是因为当您分别访问书籍或用户页面时,您会自动抓取一本新书/用户。

发生这种情况是因为页面加载,然后useEffect启动并抓取一本书或用户,然后页面需要重新渲染以将新抓取的书或用户放入 DOM。

我已经修改了您的 CodePen 以表明情况确实如此。你会看到它只渲染一次。

我还添加了一个按钮,允许您根据需要获取新书或用户...这是为了显示如何仅重新呈现您所在的页面。

总而言之,据我所知,这是预期的行为。

编辑 react-state-manager-hooks-context

我试图用不同的例子来解释希望会有所帮助。

因为上下文使用引用标识来确定何时重新渲染,所以当提供者的父级重新渲染时,这可能会在使用者中触发意外渲染。

例如:下面的代码将在每次 Provider 重新渲染时重新渲染所有消费者,因为总是为value创建一个新对象

class App extends React.Component {
  render() {
   return (
      <Provider value={{something: 'something'}}>
        <Toolbar />
      </Provider>
    );
 }
}

为了解决这个问题,将值提升到父状态

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: {something: 'something'},
    };
  }

  render() {
    return (
      <Provider value={this.state.value}>
        <Toolbar />
      </Provider>
    );
  }
}

这个用于防止组件在 React 中渲染的解决方案称为 shouldComponentUpdate。 它是一种可在 React 类组件上使用的生命周期方法。 而不是像以前那样将 Square 作为功能性无状态组件:

const Square = ({ number }) => <Item>{number * number}</Item>;

您可以使用带有 componentShouldUpdate 方法的类组件:

class Square extends Component {
  shouldComponentUpdate(nextProps, nextState) {
    ...
  }

  render() {
    return <Item>{this.props.number * this.props.number}</Item>;
  }
}

如您所见, shouldComponentUpdate 类方法在运行组件的重新渲染之前可以访问下一个 props 和 state。 这就是您可以决定通过从此方法返回 false 来阻止重新渲染的地方。 如果返回 true,组件将重新渲染。

class Square extends Component {
  shouldComponentUpdate(nextProps, nextState) {
    if (this.props.number === nextProps.number) {
      return false;
    } else {
      return true;
    }
  }

  render() {
    return <Item>{this.props.number * this.props.number}</Item>;
  }
}

在这种情况下,如果传入的数字道具没有改变,则组件不应更新。 通过将控制台日志再次添加到您的组件中来尝试一下。 当透视改变时,Square 组件不应重新渲染。 这对您的 React 应用程序来说是一个巨大的性能提升,因为您的所有子组件都不会随着其父组件的每次重新渲染而重新渲染。 最后,由您来阻止重新渲染组件。

了解这个 componentShouldUpdate 方法肯定会对你有所帮助!

暂无
暂无

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

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