React state with calculated fields

后端 未结 6 1977
半阙折子戏
半阙折子戏 2021-02-13 18:24

I have a react component, which has properties and state. Some fields of state contain input data (uplifted from input control), but there is also fields in the state that must

6条回答
  •  醉话见心
    2021-02-13 18:57

    If you are using React 16.8.0 and above, you can use React hooks API. I think it's useMemo() hook you might need. For example:

    import React, { useMemo } from 'react'
    
    const MyComponent = ({ ...props }) => {
      const calculatedValue = useMemo(
        () => {
          // Do expensive calculation and return.
        },
        [a, b]
      )
    
      return (
        
    { calculatedValue }
    ) }

    For more details, refer to the React documentation

提交回复
热议问题