简体   繁体   English

React context api 仅呈现使用已更改值的组件

[英]React context api only render components that use values that have changed

I was messing with the context api and I was passing a couple of values down through the tree, lets call them val and val2.我弄乱了上下文 api,我正在通过树向下传递几个值,让我们称它们为 val 和 val2。 And I have three different components let just call them First, Second, and Third.我有三个不同的组件,姑且称之为 First、Second 和 Third。 Second and Third will be a consumer of the Context Api in React. Second 和 Third 将是 React 中 Context Api 的使用者。 Second will get the first val and third will get the second val2.第二个将获得第一个 val,第三个将获得第二个 val2。 Now if I have a function that changes the first val that the second component uses I am getting a render of the Third component that uses the val2 value.现在,如果我有一个更改第二个组件使用的第一个 val 的函数,我将获得使用 val2 值的第三个组件的渲染。 With redux I don't get this unwanted rendering.使用 redux 我不会得到这种不需要的渲染。 I was wondering if there was a way to bypass this rendering in react or if redux is still the way to go to get this type of performance.我想知道是否有办法绕过 react 中的这种渲染,或者 redux 是否仍然是获得这种性能的方法。 Here is a little markup of what I am talking about:这是我正在谈论的内容的一些标记:

The state and global context are held in a wrapper wrapping the first component which wraps the second and third component.状态和全局上下文保存在包装第一个组件的包装器中,该包装器包装第二个和第三个组件。

Here is the second component:这是第二个组件:

import React, { useContext, useState } from 'react';
import {GlobalContext} from '../context/GlobalState'

const Second = () => {
  console.log("Second Rendered");
  const context = useContext(GlobalContext);
  const [inputVal, updateVal] = useState('');
  const handleSubmit = e => {
    e.preventDefault();
    context.updateVal(inputVal)
  }
  return (
    <>
      <p>{context.val}</p>
      <form onSubmit={handleSubmit}>
        <input onChange={(e) => updateVal(e.target.value)} name="val" value={inputVal} />
        <button type="submit">Update Val</button>
      </form>
    </>
  )
}

export default Second;

And Here is the third component:这是第三个组成部分:

import React, { useContext} from 'react';
import {GlobalContext} from '../context/GlobalState';

const Third = () => {
  console.log("Third Rendered")
  const context = useContext(GlobalContext);

  return (
    <p>{context.val2}</p>
  )
}

export default Third;

So when I update the value of the second component the third component renders even though the values are not shared.因此,当我更新第二个组件的值时,即使这些值未共享,也会呈现第三个组件。 I understand why its happening I just was curious if there was a way to stop this from happening.我明白为什么会发生这种情况我只是很好奇是否有办法阻止这种情况发生。 Like I said I don't get this behavior from redux so I guess I'm wondering if I should just stick with redux for state management.就像我说的那样,我没有从 redux 中得到这种行为,所以我想我想知道我是否应该坚持使用 redux 进行状态管理。

I understand that you need to avoid re-renders in your <Third /> component.我知道您需要避免在<Third />组件中重新渲染。 In this case, you can use React.memo with functional components, it is the same functionality as pure components but without class.在这种情况下,您可以将React.memo与功能组件一起使用,它与纯组件具有相同的功能,但没有类。

const MyComponent = React.memo(function MyComponent(props) {
  /* render using props */
});

So, you can't use memo on the Component Third (because it's not the props that are being changed but the Context) but you can split Third into two pieces - one that uses Context and one that receives the necessary value from a prop - and memoize that inner one.所以,你不能在组件 Third 上使用备忘录(因为它不是正在改变的道具,而是 Context)但是你可以将 Third 分成两部分 - 一个使用 Context,一个从道具接收必要的值 -并记住内心的那个。 The outer Component is going to rerender but it's just a wrapper so that's not a big deal.外部 Component 将要重新渲染,但它只是一个包装器,所以这没什么大不了的。

For instance:例如:

import React, { useContext} from 'react';
import {GlobalContext} from '../context/GlobalState';

const Third = () => {
  console.log("Third Rendered")
  const context = useContext(GlobalContext);

  return (
    <ThirdBody val={context.val2} />
  )
}

const ThirdBody = React.memo(({ val }) => <p>{val}</p>)

export default Third;

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

相关问题 React Context api - 消费者在上下文更改后不重新呈现 - React Context api - Consumer Does Not re-render after context changed React-native是否不应该仅渲染类似于React工作方式的更改组件? - Shouldn't React-native render only changed components similar to how React works? React Hooks:在上下文 API 中存储的值发生更改后渲染组件? - React Hooks: Render component after the value stored in context API has changed? 当状态被其他使用React Context API的组件更改时,如何防止自动渲染? - How to prevent automatic render when state changed by some other component that uses React Context API? React组件可以在浏览器中正确渲染,但是Jest在渲染时会测试错误:“只有ReactOwner可以具有引用” - React components render correctly in browser, but Jest test errors when rendering: “Only a ReactOwner can have refs” 如何在启动时只渲染一次 React 组件? - How to render React components only once on startup? 如何仅在反应中呈现子组件 - How to render child components only in react 如果 props 改变,React Native 组件不会重新渲染 - React Native components not re-render if props changed 无法在深层后代组件中使用 React Context - Unable to use React Context in deep descendant components 反应:上下文的变化不会重新渲染我的组件 - react : changes on context does not re-render my components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM