javascript - 更新 React Context,而不重新渲染进行更新的组件

标签 javascript reactjs react-hooks react-context

我有一个上下文和 2 个组件:一个用于显示上下文中的内容,另一个用于更新它。

通过在更新程序组件中添加以下代码,它将在更改上下文时重新渲染。

const [, setArray] = React.useContext(context);
setArray(prevArray => { return [...prevArray, []] }

这意味着 infitie 会重新渲染。我需要避免这种情况。由于更新程序不使用上下文中的数据,因此不应更新。

完整示例:我正在存储和显示有关组件的探查器数据。

https://codesandbox.io/s/update-react-context-without-re-rendering-the-component-making-the-update-k8ogr?file=/src/App.js

const context = React.createContext();

const Provider = props => {
  const [array, setArray] = React.useState([]);

  const value = React.useMemo(() => [array, setArray], [array]);

  return <context.Provider value={value} {...props} />;
};

const Metrics = () => {
  const [array] = React.useContext(context);

  return <TextareaAutosize value={JSON.stringify(array, null, 2)} />;
};

const Component = () => {
  const [, setArray] = React.useContext(context);

  const onRenderCallback = (id, _phase, actualDuration) => {
    setArray(prevArray => {
      return [...prevArray, [id, actualDuration]];
    });
  };

  return (
    <React.Profiler id="1" onRender={onRenderCallback}>
      <div />
    </React.Profiler>
  );
};

export default function App() {
  return (
    <div className="App">
      <Provider>
        <Metrics />
        <Component />
      </Provider>
    </div>
  );
}

最佳答案

这是我使用以下文章得出的结论:https://kentcdodds.com/blog/how-to-optimize-your-context-value

使用 2 个上下文,一个用于存储状态,一个用于更新状态:

const stateContext = React.createContext();
const updaterContext = React.createContext();

const array = React.useContext(stateContext);
const setArray = React.useContext(updaterContext);

完整示例: https://codesandbox.io/s/solution-update-react-context-without-re-rendering-the-component-making-the-update-yv0gf?file=/src/App.js

import React from "react";
import "./styles.css";
import TextareaAutosize from "react-textarea-autosize";

// https://kentcdodds.com/blog/how-to-optimize-your-context-value
const stateContext = React.createContext();
const updaterContext = React.createContext();

const Provider = props => {
  const [array, setArray] = React.useState([]);

  return (
    <stateContext.Provider value={array}>
      <updaterContext.Provider value={setArray}>
        {props.children}
      </updaterContext.Provider>
    </stateContext.Provider>
  );
};

const useUpdaterContext = () => {
  return React.useContext(updaterContext);
};

const Metrics = () => {
  const array = React.useContext(stateContext);

  return <TextareaAutosize value={JSON.stringify(array, null, 2)} />;
};

const Component = () => {
  const setArray = useUpdaterContext();

  const onRenderCallback = (id, _phase, actualDuration) => {
    setArray(prevArray => [...prevArray, [id, actualDuration]]);
  };

  return (
    <React.Profiler id="1" onRender={onRenderCallback}>
      <div />
    </React.Profiler>
  );
};

export default function App() {
  return (
    <div className="App">
      <Provider>
        <Metrics />
        <Component />
      </Provider>
    </div>
  );
}

关于javascript - 更新 React Context,而不重新渲染进行更新的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62038538/

相关文章:

javascript - 满足以下所有示例的更经济的正则表达式 :

reactjs - React Native 样板模板

reactjs - React Nuka slider 在哪里放置装饰器?

reactjs - useEffect 中的 Debounce/Throttle onChange 事件

javascript - 检查当前目录是否包含package.json以及谁的名字等于值

javascript - IE9+ : poor performance while dealing with DOM

javascript - 获取li内子标签的html内容

javascript - React 中如何让父组件管理子组件的状态?

javascript - 如何将焦点设置到下一个输入上的输入键按下 react js与refs

reactjs - 为什么 useState 不使用 keydown 处理程序更新状态?