iuliu.net iuliu.net -3 years ago 211
React JSX Question

Why should you `extend` Component in React (Native)?

I'm quite new in this react ecosystem, but it's pretty clear so far on what a component is and how to create one using:

export default class NotificationsScreen extends Component {
render() {
return(<View></View>);
}
}


however I've seen some examples that just use

const MySmallComponent = (props) => <View></View>


And the app seems to work just as fine..

What is the advantage of extending
Component
?

Answer Source

Since React is strongly connected to functional programming, it's always a good habit to write pure functions in React. If your component doesn't need to manage state or involve lifecycle methods then use stateless component:

  1. It just feels more natural that way
  2. Easier to write, easier to read
  3. No extends, state and lifecycle methods mean faster code

You can even find more reasons at this article. So, all I want to say is, always use stateless component if you don't need to manage its state.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download