In React Native (and React in general), components are the building blocks of your user interface. Historically, there were two main ways to define components: class components and functional components. However, with the introduction of React Hooks, functional components have become the dominant and recommended approach. Here's a breakdown:
Class Components:
React.Component
.render()
method that returns the JSX (JavaScript XML) that defines the component's UI.this.state
.componentDidMount
, componentDidUpdate
, componentWillUnmount
), which allow you to perform actions at specific points in the component's lifecycle.import React, { Component } from 'react';
import { Text } from 'react-native';
class MyClassComponent extends Component {
render() {
return <Text>Hello from a class component!</Text>;
}
}
export default MyClassComponent;
Functional Components:
useState
and perform side effects using useEffect
, effectively replicating the capabilities of class components.import React from 'react';
import { Text } from 'react-native';
const MyFunctionalComponent = () => {
return <Text>Hello from a functional component!</Text>;
};
export default MyFunctionalComponent;
Key Differences and the Impact of Hooks :
useState
hook.useEffect
hook.