简体   繁体   English

React.useMemo 与 React.useState

[英]React.useMemo vs React.useState

I see other ask about useState + useEffect .我看到其他人询问useState + useEffect but I'm wonder what is the different for unchanged (const) variable in the two implementation:但我想知道这两个实现中未更改的(const)变量有什么不同:

function optionA(){
  const [Mesh]  = useState(()=> new Mesh)
  
 return "...someRender"
}

function optionB(){
 const Mesh = useMemo(()=> new Mesh)

 return "...someRender"
}

Edited:编辑:

I know useRef(someValue) is more fit for that but I find it "undeclaratively" to write Mesh.current anywhere我知道useRef(someValue)更适合这个,但我发现它“不明确地”在任何地方写Mesh.current

The key bit is where you said "unchanged."关键是你说“不变”的地方。 There's no point in having a value in state that never changes.在 state 中设置一个永远不会改变的值是没有意义的。 The purpose of state is to hold information that, when changed , will cause a rendering update. state 的目的是保存信息,这些信息在更改时会导致渲染更新。 If you never change it, there's no reason to put it in state.如果你从不改变它,就没有理由把它放在 state 中。

But , don't use useMemo for this.但是,不要为此使用useMemo From the documentation:从文档中:

You may rely on useMemo as a performance optimization, not as a semantic guarantee.您可能依赖 useMemo 作为性能优化,而不是语义保证。 In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, eg to free memory for offscreen components.将来,React 可能会选择“忘记”一些以前记忆的值并在下一次渲染时重新计算它们,例如为屏幕外组件释放 memory。 Write your code so that it still works without useMemo — and then add it to optimize performance.编写您的代码,使其在没有useMemo的情况下仍能正常工作——然后添加它以优化性能。

(their emphasis) (他们的重点)

If you have information you want to hold statically for the entire lifetime of your component, use useRef , not useMemo :如果您有想要在组件的整个生命周期内静态保存的信息,请使用useRef而不是useMemo

function Example() {
    const refMesh = useRef(null);
    if (!refMesh.current) {
        // One-time initialization
        refMesh.current = new Mesh();
    }
    const mesh = refMesh.current;
    // ...use `mesh` from here on out...
}

Unlike useMemo , you can be certain that only a single Mesh instance is created during the lifecycle of the component.useMemo不同,您可以确定在组件的生命周期内只会创建一个Mesh实例。 (This also has the other slight advantage that you aren't constantly creating a function to pass useMemo on each render.) (这还有一个小优点,即您不必不断创建 function 来在每次渲染时传递useMemo 。)

I sometimes use a single ref storing an object to hold multiple pieces of instance information that isn't appropriate for state (including stable callback functions for child components).我有时会使用存储 object 的单个 ref 来保存不适合 state 的多条实例信息(包括子组件的稳定回调函数)。 It's quite handy.这很方便。


Re your edit:重新编辑:

I know useRef(someValue) is more fit for that but I find it "undeclaratively" to write Mesh.current anywhere我知道useRef(someValue)更适合这个,但我发现它“不明确地”在任何地方写Mesh.current

You don't.你不知道。 See the const mesh = refMesh.current;const mesh = refMesh.current; above, you'd use mesh , not refMesh.current , throughout the remainder of the component.在上面,您将在整个组件的其余部分使用mesh而不是refMesh.current

If you find the if awkward, you can wrap this in a hook that provides the semantic guarantee that useMemo doesn't provide, at the cost of creating a function on every render (as with useMemo , useCallback , etc.):如果你发现if很尴尬,你可以将它包装在一个钩子中,它提供了useMemo不提供的语义保证,代价是在每个渲染上创建一个 function(与useMemouseCallback等):

const useData = (instanceInitializer) => {
    const ref = useRef(null);
    if (!ref.current) {
        ref.current = instanceInitializer();
    }
    return ref.current;
};

Then it would be:那么它将是:

function Example() {
    const mesh = useData(() => new Mesh());
    // ...use `mesh` from here on out...
}

That has the (very) slight extra cost of the function creation, but better ergonomics.与 function 的创建相比,这有(非常)轻微的额外成本,但更好的人体工程学。

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

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