an array index? Use splice or push If you must be alter the array
My problem is that I'm not doing that :)) let history = [] const [shapes, setShapes] = useState([]) const handleUndo = useCallback(() => { if (!history.length) return history.pop() let isFound = false for (let i = shapes.length - 1; i >= 0; i--) { if (!history[history.length - 1]) return if (shapes[i].id === history[history.length - 1].id) { isFound = true shapes[i] = history[history.length - 1] break } } if (!isFound) { shapes.pop() } setShapes(shapes) }, [shapes])
Oh they use the index as identifier for another array. Why not use the id property? It seems like a duplicated check. Unnecessary complexity
heyyo how do you know when to use () or {} in code? [] is for array but I get confused with other brackets
The code that you saw above is react not vanila js :)
it's language syntax, you have to learn it according to the docs
Ok i guess i shud focus more on docs ...
Learn primitive types vs reference types conditionals, guard clauses loops functions, passing functions to functions, returning functions from functions, arrow functions, iife array methods prototype chain event loop Destructing, spread Dom manipulation Promises, async await Make something small with it then you should be ready for a framework
Обсуждают сегодня