setState is async, and you need to invoke the function in the callback.
this.setState({
languages: [...langs]
}, () => console.log(this.state.languages)
);
Answer from Stan Luo on Stack OverflowsetState is async, and you need to invoke the function in the callback.
this.setState({
languages: [...langs]
}, () => console.log(this.state.languages)
);
The second parameter is a callback - you're executing console.log immediately and passing its result as the callback. Change you code to this:
this.setState({
languages: [...langs]
},
() => console.log(this.state.languages);
);
SetState in an array with React spread operator
Spread array of objects to array of objects react
How do you update a value inside an array of objects with useState?
Can you spread an array of components in React?
, T extends Element>( type: string, props?: ClassAttributes & P, ...children: ReactNode[]): DOMElement; Notice the children are variadic, and note that a child can be an array. In JSX, {...components} happens to transpile to...the same thing actually: {...components}. This is an object literal, and is not a valid child type. This works:
{components} More on reddit.comMany ways to do it, really. Here's one:
this.setState(prevState => (
{appointmentsData: {...prevState.appointmentsData}, newAppt}
));
Though your approach should work too, if you just add the appointmentsData: first. Like so:
this.setState({appointmentsData: {...this.state.appointmentsData, ...newAppt}});
This will spread all the previous values into a new object, plus the one you want to add.
I personally always use the functional way of setting the state when the new one depends on a previous state value, but your way is fine here too.
You should not use spread operator on newAppt. Just go with:
this.setState({ ...this.state.appointmentsData, newAppt});. It seems to me that the spread operator on newAppt is not getting any data for the const.
Simply assign the property and it will overwrite the old one.
export default function(state = initialState, action) {
switch (action.type) {
case GET_BOOKS:
console.log(action.payload.results);
// spread current state and inaddition to that set new books data
// which overwrites books property from old state
return { ...state, books : action.payload.results };
// spread --^^^^^^^^^^^^^^^^^^^^^^^^^^^---
default:
return state;
}
}
UPDATE : If you want to concatenate it with existing then do something like this.
export default function(state = initialState, action) {
switch (action.type) {
case GET_BOOKS:
console.log(action.payload.results);
return { ...state, books : [...state.books, ...action.payload.results] };
default:
return state;
}
}
FYI : The ...state part is for copying other state properties(assumes there exists other state values)
You need to concat current state and data coming from api
return { books: [...state.books, ...action.payload.results] };
Complete Code
import { GET_BOOKS } from "../actions/types";
const initialState = { books: [] };
export default (state: Object = initialState, action: Object) => {
switch (action.type) {
case GET_BOOKS:
return { books: [...state.books, ...action.payload.results] };
default:
return state;
}
};