Answer the question
In order to leave comments, you need to log in
Is it necessary to use useMemo in hooks?
i created useBanner hooks
const useBanner = (array, yardage) => {
const [bannArr, setBannArr] = useState(array.slice(0, yardage));
const [bannListIndex, setBannIndex] = useState(1);
return {
....
};
};
const Banner= ({
array,
yardage
}) => {
const { bannForth, bannBeck, bannArr } = useBanner(array, yardage);
return (
...
);
};
const useBanner = (array, yardage) => {
const [bannArr, setBannArr] = useState([]);
const [bannListIndex, setBannIndex] = useState(1);
useMemo(() => {
setBannArr(array.slice(0, yardage));
setBannIndex(1);
}, [array, yardage]);
return {
....
};
};
Answer the question
In order to leave comments, you need to log in
For expensive operations, it's better to use lazy state initialization:
const useBanner = (array, yardage) => {
const [bannArr, setBannArr] = useState(() => array.slice(0, yardage));
const [bannListIndex, setBannIndex] = useState(1);
return {
....
};
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question