Wen Seng Tee
Wen Seng Tee

Reputation: 95

Initialize state - difference between using a constructor and not - React Native

Using Constructor:

import { Text } from 'react-native';
import Component from 'react';

class Blink extends Component {
    constructor(props) {
      super(props);
      this.state = {test: "Hello"};
}

Without constructor:

import { Text } from 'react-native';
import Component from 'react';

class Blink extends Component {
    state = { test:"Hello" }
}

The code works in the same way. But what's the difference? Which one is better?

Upvotes: 3

Views: 5100

Answers (1)

sshaul
sshaul

Reputation: 99

It's just a matter of preference! Here's an article I found about the different ways to initialize a component: https://daveceddia.com/where-initialize-state-react/

Upvotes: 5

Related Questions