can anyone tell me diffrence between making class like this and
class Fruits extends React.Component
const fruits = ()=>{
}
and like this we make
can anyone tell me diffrence between making class like this and
class Fruits extends React.Component
const fruits = ()=>{
}
and like this we make
One is a class and one is a function.
A class relates to Object-Oriented Programming, where data and the procedures for manipulating that data are bound together in one container (an Object). With respect to React that means you have some state, and you have some functions available to you for manipulating that state (componentDidMount
, render
etc) . When the render
method is ran, the JSX defined inside it is ran and the result of that is returned.
A function takes some input and generates some output. With respect to React, it takes some props (an object) and runs the JSX define inside the body of the function, returning the result of that.
Note that React now has hooks which allow functions to have some internal state. You don’t really need classes any more except in one specific use case (error boundaries). Function components can now do everything that class-based components can do. At some point the class-based syntax will be moved out of React itself into an external library, it’ll just be functions, but that won’t be for a while.