My question is, what should I modify on the interface so the number before the object (0, 1, ...) is generated automatically when the user click Add?
export interface IRoute { amount?: string; id?: number; location?: string }export const initialRoute: IRoute = { amount: '', id: 0, location: ''} const [route, setRoute] = useState<IRoute[]>([initialRoute]); const nextId = route[route.length - 1].id + 1; const onAdd = () =>{[...prevState, {amount: '54', id: nextId, 'text'}]}
When the user clicks onAdd the response should look like this:
expected Behaviour:
route: [ {0:{amount: '45', id: nextId , location: 'Test'}}, {1:{amount: '35', id: nextId , location: 'Test'}} ]
actual Behaviour
route: [ {amount: '45', id: nextId , location: 'Test'}, {amount: '35', id: nextId , location: 'Test'} ]