codingtcher.com
Theme Night
Home
Login

Add element to array react hooks

Home » Tutorial Post » Add element to array react hooks

add object to array react hook


const [theArray, setTheArray] = useState(initialArray);


adding to array using reach hooks

setMyArray(oldArray => [...oldArray, newElement]);

add object to array react hook

setTheArray([...theArray, newElement]);
Source: stackoverflow.com

add items to a react array in hooks

const addMessage = (newMessage) => setMessages(state => [...state, newMessage])
Source: stackoverflow.com

Related Post