N
N
Nikolai2022-04-19 09:38:14
JavaScript
Nikolai, 2022-04-19 09:38:14

How to add element to array in react object?

Conditionally there is such a state

const [filter, setFilter] = useState({type:"", duration: []});


how to add values ​​from different buttons to this array?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2022-04-19
@moClay

const { duration } = filter;

setFilter({
  ...filter,
  [ ...duration, 1, 2, 3 ], // добавили в массив 1, 2 и 3
});

// теперь state станет
// { type:"", duration: [1, 2, 3] }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question