Reputation: 551
ComponentWillMount has been renamed and deprecated, and is not recommended to be used
constructor(props) {
super(props);
this.state = {
cartItems: []
};}
componentWillMount() {
AsyncStorage.getItem("CART", (err, res) => {
if (!res) this.setState({ cartItems: [] });
else this.setState({ cartItems: JSON.parse(res) });
});}
what should I do to fetch the cartItems before rendering ??
Upvotes: 0
Views: 751
Reputation: 21317
There is a rule of thumb. Does your old code (which implements componentWillMount
) performs any side effect? Case no it's just an initialization and you can do it inside constructor
. Case you need to perform a side effect (API call for example) you should use componentDidMount
instead
state = { cartItems : [] }
componentDidMount() {
AsyncStorage.getItem("CART", (err, res) => {
if (!res) this.setState({ cartItems: [] });
else this.setState({ cartItems: JSON.parse(res) });
});
}
Upvotes: 3