

function BookCreate({ onCreate }) {
const [title, setTitle] = useState("");
const handleChange = (e) => {
e.preventDefault();
setTitle(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
onCreate(title);
setTitle(" ");
};
return (
...
<form onSubmit={handleSubmit}>
<input
className="input"
value={title}
onChange={handleChange}
type="text"
/>
<button className="button">submit</button>
</form>
...
);
}
const [books, setBooks] = useState([]);
Js sees you want to create an array =>
It finds an empty spot in memory and puts and array there
React remembers this array for future use
(A case :Reference to current state)
books.push({id:123, title:title})
setBooks(books)
//books.length => stays at '0'
3.push modifies the existing array => JS finds the existing array and adds an element
(B case :Reference to 'new' state)
setBooks(books) **should be rerenderd
=> React gets a reference to the 'new' state
'A' 'B' cases are poiting at the same array/object
=> No render
const updateBooks = []
const updatedBooks = [
...books,
{ id: Math.round(Math.random() * 9999), title },
];
setBooks(updatedBooks);
=> Referenes point at different array/ objects
=> React processes 'rerender'
Do not mutate /change/ modify / array or objects
Adding in the middle => slice
const addColor = (new, index) =>{
const updateColors = [
...colors.slice(0,index),
new,
...colors(index)
]
setColors[updateColors]
}
Use filter
filter is added to new array
Use Map
const {color, ...rest} =fruit
setFruit(rest)
rest => Make a new object
Math.roung(Math.random()*9999)