Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Class and Const when creating UI in react-Native?

const App = () => (
  <View>
    <Text>Test</Text>
  </View>
  )

class App extends Component {
  render() {
    return (
      <View>
        <Text>Test</Text>
      </View>
    );
  }
}

When I test, two things are the same. Please tell me the difference between these two.

like image 614
oijafoijf asnjksdjn Avatar asked Mar 06 '26 13:03

oijafoijf asnjksdjn


1 Answers

A Class Component is a stateful component and const App is a stateless (or functional) component.

A stateful component is used to:

  1. initialize the state
  2. modify the state
  3. render something

Additionally it has lifecycle methods.

Whereas a stateless component is often just used to return a piece of UI.

In short: a class component is more powerful than a functional component

EDIT:

Since React Native 0.59 also functional components can have a state. See Hooks-Intro for more information.

like image 177
Tim Avatar answered Mar 08 '26 09:03

Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!