4 ms·
> As someone who works mostly in React I've never found a use for classes. You can't NOT use classes if you use React. [Edit: For components with state] your
by Lazare 10y ago
> As someone who works mostly in React I've never found a use for classes.
You can't NOT use classes if you use React. [Edit: For components with state] your syntax choices are:
Foo = React.createClass({})
Or:
class Foo extends React.Component {}
But both are examples of JS classes; it's just a question of whether you use the class keyword or not. Every React component [Edit: with state] is a class.
Edit: Fixed silly error; obviously you can use stateless functional components and I do all the time, but you can't avoid classes with React.
- ng12 10y agoSure, I do define classes but that's because React expects it -- I don't actually use them as classes. My React classes would function the same as plain old objects. I never use constructors, or inheritence, or new, or any of the features mentioned in this post.
- shepmaster 10y agoI tend to heavily favor functional components[1]: function Welcome(props) { return <h1>Hello, {props.name}</h1>; } or a little nicer: const Welcome = ({ name }) => ( <h1>Hello, {name}</h1> ) [1]: https://facebook.github.io/react/docs/components-and-props.html#functional-and-class-components https://facebook.github.io/react/docs/components-and-props.h...
- Lazare 10y agoSorry, typed too fast. Edited my answer to clarify.
- armandososa 10y agowell, no. const Foo = props => <div {...props}/> Is a valid "stateless component". They are just the render function.