Here is Dan Abramov’s excellent article , Presentation and Container Components , that can help you with this.
But the troll as I understand it:
You will have to use class CreatePostForm extends Component {} or React.createClass() if:
- you need access to the life cycle methods of your component (i.e.: componentWillMount or componentDidMount) - NOTE. Starting with React 16.8 , this is no longer necessarily true, and I highly recommend reading on React Hooks, as they can simplify things as soon as you get comfortable with them;
- your component has direct access to your store and thus retains state (some people also call this type of component, smart component or container).
When your component simply receives the details and displays them on the page, then you have a “stateless component” (some people call these components stupid components or presentation components) and you can use a pure function to represent it, and it can be that simple. like this
import React from 'react'; export default() => <p>Hello from React!</p>;
Now it’s important to remember that a pure function can become much more complex than this, and if you are familiar with some ESNext syntax and attributes of destructuring and distribution, you can have a presentation component that looks like this:
import React from 'react'; import AnotherComponent from './AnotherComponent'; export default ({ children, ...rest }) => <AnotherComponent extraProp="anExtraProp" { ...rest }> { children } </AnotherComponent>;
Hope this helps.
source share