Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

componentWillMount is deprecated and will be removed in the next major version 0.54.0 in React Native

I use the react native latest version of 0.54.0 and Whenever run the apps on iOS there is found warning about deprecate the lifecycle methods. and also please update the components.

Warning :

componentWillMount is deprecated and will be removed in the next major version. Use componentDidMount instead. As a temporary workaround, you can rename to UNSAFE_componentWillMount. Please update the following components: Container, Text, TouchableOpacity, Transitioner, View

I Have also change according to the waring add prefix UNSAFE_ each of the method.

UNSAFE_componentDidMount() { } UNSAFE_componentWillMount() { } UNSAFE_componentWillUpdate(nextProps, nextState) { } UNSAFE_componentWillReceiveProps(nextProps) { } 

Although the warning continue. Please help me.

Currently I have hide the YellowBox waring in my Apps.

import { YellowBox } from 'react-native';  render() {    YellowBox.ignoreWarnings([     'Warning: componentWillMount is deprecated',     'Warning: componentWillReceiveProps is deprecated',   ]); } 
like image 340
Kirit Modi Avatar asked Mar 10 '18 06:03

Kirit Modi


People also ask

Is componentWillMount deprecated?

Our React teaching team follow developments in the Facebooks UI-creating library very closely. We update our curriculum every 2 weeks, and recently they came across an update worth sharing with everyone! This is a blog for anyone who builds User Interfaces with React!

What is componentWillMount replaced by?

The function is replaced with class constructor.

What is componentWillMount in React native?

The componentWillMount() method allows us to execute the React code synchronously when the component gets loaded or mounted in the DOM (Document Object Model). This method is called during the mounting phase of the React Life-cycle.

How do you use componentWillMount method in React?

Using componentWillMount() to Make API Calls One of the primary usages of componentWillMount() is to make API calls once the component is initiated and configure the values into the state. To make an API call, use an HttpClient such as Axios , or or you can use fetch() to trigger the AJAX call.


2 Answers

You should move all the code from the componentWillMount to the constructor or componentDidMount.

componentWillMount() is invoked just before mounting occurs. It is called before render(), therefore calling setState() synchronously in this method will not trigger an extra rendering. Generally, we recommend using the constructor() instead. Avoid introducing any side-effects or subscriptions in this method. For those use cases, use componentDidMount() instead. This is the only lifecycle hook called on server rendering.

componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. This method is a good place to set up any subscriptions. If you do that, don’t forget to unsubscribe in componentWillUnmount(). Calling setState() in this method will trigger an extra rendering, but it will happen before the browser updates the screen. This guarantees that even though the render() will be called twice in this case, the user won’t see the intermediate state. Use this pattern with caution because it often causes performance issues. It can, however, be necessary for cases like modals and tooltips when you need to measure a DOM node before rendering something that depends on its size or position.

From the official docs

like image 60
Vladimir K. Avatar answered Oct 07 '22 05:10

Vladimir K.


componentDidMount isn't deprecated and is definitely still safe to use, so there's no need to add UNSAFE_ to that method. The componentWillSomething methods are the ones that seem to be on their way out. Instead of componentWillMount, use a constructor for the stuff that doesn't produce side-effects, and use componentDidMount for the stuff that does.

like image 27
Elliott Jones Avatar answered Oct 07 '22 06:10

Elliott Jones