Answer the question
In order to leave comments, you need to log in
Why is new data not being written to state?
Why is an empty string written to state and not all values from localStorage?
export class Cart extends React.Component {
state = {
arrFromLocal:'',
}
componentDidMount(){
var values = [],
keys = Object.keys(localStorage),
i = keys.length;
while ( i-- ) {
values.push(localStorage.getItem(keys[i]) );
}
this.setState({
arrFromLocal: values
},console.log(this.state.arrFromLocal, values))
}
Answer the question
In order to leave comments, you need to log in
The state is updated asynchronously. The callback is passed as the second argument.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question