Answer the question
In order to leave comments, you need to log in
Does the useCallback hook optimize the component if we don't pass a function in a prop?
I was interviewing for a well-known taxi service. I was asked to write an input field component. I did this:
import React, { useState } from 'react';
const Input = () => {
const [value, setValue] = useState('');
const handleChange = (e) => {
setValue(e.target.value);
};
return <input type="text" value={value} onChange={handleChange} />;
};
export default Input;
import React, { useState, useCallback } from 'react';
const Input = () => {
const [value, setValue] = useState('');
const handleChange = useCallback((e) => {
setValue(e.target.value);
}, []);
return <input type="text" value={value} onChange={handleChange} />;
};
export default Input;
useCallback
a factory is created with the help, it remains alone, and each of its calls returns a new function. That is, there is still a point in optimization. useCallback
optimize the component if we don't pass a function in a prop? Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question