const [state, setState] = useState({id:1, firstName:"taro", lastName:"sato"});
const obj = {firstName:"hanako", lastName:"sato"};
const pushFunction = (state) => {
setState( [...state, {id:2, ...obj}] )
}
// ...state => {id:1, firstName:"taro", lastName:"sato"}
// ...arr => firstName:"hanako", lastName:"sato"
// id:2, ...arr => id:2, firstName:"hanako", lastName:"sato"
// [...state, {id:2, ...obj}] => [{id:1, firstName:"taro", lastName:"sato"}, {id:2, firstName:"hanako", lastName:"sato"}]
More than 3 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme