https://reactjs.org/docs/optimizing-performance.html from the app is a very extensive subject. The question title concerns optimizing rendering, and this can be taken to two interpretations: avoiding unnecessary rendering or rendering rendering computationally less costly.The body of the question demonstrates a concern about the declaration of functions or constants in the functional component, which are re-declared in all rendering. This will be the focus of the answer written here.When does re-rendering occur?It is important to know when a re-rendering occurs before trying to optimize this. The author of the question already seems to know about, but follows a brief summary. Re-rendering occurs when:The root node of the tree is of different type (e.g. <div> <a>Oi</a> </div> and <button> <a>Oi</a> </button>);Some attribute has been modified (e.g. <div className="a" /> and <div className="b" />);Some component property has been modified (e.g. <Timer time={10} /> and <Timer time={15} />);Some state of the component has been modified;The comparison of us children finds differences. This comparison is a recursion in both trees at the same time, comparing knot to knot.Source of the quote: https://pt.stackoverflow.com/a/514307/100416 How to avoid (or improve) re-renderings on functional components?React.memoThe question https://reactjs.org/docs/react-api.html#reactmemo . It can be used to avoid re-rendering based on Props received. If your functional component you use React.memo also use useState, useReducer or useContext, it will continue to re-address in state or context changes.function MyComponent(props) {
// ...
}
function areEqual(prevProps, nextProps) {
// Retorne true se nextProps retornaria o mesmo resultado a ser renderizado
// que prevProps, senão retorne false
}
export default React.memo(MyComponent, areEqual);
useCallbackYou can use the hook https://reactjs.org/docs/hooks-reference.html#usecallback to maintain the reference of a function, since a new reference is created to each render. O useCallback returns a callback memoized. When is that useful? Well, when that function is in https://pt.stackoverflow.com/q/514236/100416 from another hook.It also ends up serving for the standard comparison of the props performed by React, since it will realize that it is the same function. But if you are to use useCallback thinking about it, it might be simpler to use React.memo in the child component and compare only the necessary properties, depending on your needs. If this function really is changeable, then the useCallback can make sense.const memoizedCallback = useCallback(
() => {
doSomething(a, b);
},
[a, b],
);
useMemoO https://reactjs.org/docs/hooks-reference.html#usememo returns a memoized value. The documentation recommends the use of this hook for computationally expensive calculations. Note that the code will run during rendering, so if it's something too time-consuming, the screen will be locked and you should solve it otherwise.If you have an expensive calculation that changes only from time to time, according to variables (also have an array of dependencies), or that is always the same value, the useMemo can yes be a solution to make re-rendering cheaper. In this case, useMemo will not avoid new re-renderings, has nothing to do with it, it will only make them cheaper.const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
Note: In the future, React can choose "forget" some memoized values and recalculate them in the next render to, for example, release memory of components that are not on screen.I made a more detailed description of the useCallback and useMemo https://pt.stackoverflow.com/a/515273/100416 If you want more details about it.useRefThe statement of constants is a concern pointed out in the question. It is possible to use useMemo to maintain the reference of an object or array, but there are options that can be more suitable for this, as pointed out https://blog.logrocket.com/rethinking-hooks-memoization/ .As you do not want to memoize a value in this case, but rather keep the reference, can make use of https://reactjs.org/docs/hooks-reference.html#useref . O useRef is like a "box" that stores a changeable value in the property .current.Even, it's in https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables the answer to your doubt about whether there is something in functional components such as the declaration of constants in classes, in free translation:The "ref" object is a generic container whose current property is changeable and can contain any value, similar to an instance property in a class.Example:function Bla() {
// Inadequado:
// const baz = useMemo(() => [1, 2, 3], [])
// Adequado:
const { current: baz } = useRef([1, 2, 3])
return <Foo baz={baz} />
}
It's not so simple to improve performance. You need to understand what's getting in the way before you try to fix it or you can end up getting worse. https://pt.stackoverflow.com/q/448204/100416 .But when it is to modify something, I believe that this response has well addressed the points for functional components with the three hooks cited (useCallback, useMemo and useRef) and HOC React.memo.