Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

React Native - Type Script - Object should be changeble to a number when user clicks add

$
0
0

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'}      ]

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>