Reputation: 95
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
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