Answers for "React Hooks append state"

5

React Hooks append state

setTheArray(currentArray => [...currentArray, newElement])
Posted by: Guest on November-07-2020
14

usestate array push

setTheArray([...theArray, newElement]);
Posted by: Guest on June-02-2020
1

functional component how to add to existing array react

const {useState, useCallback} = React;
function Example() {
    const [theArray, setTheArray] = useState([]);
    const addEntryClick = () => {
        setTheArray([...theArray, `Entry ${theArray.length}`]);
    };
    return [
        <input type="button" onClick={addEntryClick} value="Add" />,
        <div>{theArray.map(entry =>
          <div>{entry}</div>
        )}
        </div>
    ];
}

ReactDOM.render(
    <Example />,
    document.getElementById("root")
);
Posted by: Guest on August-25-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language