Stateless presentational components in React
With React your UI is built out of
reusable components. It’s a modular way of development. In v0.14 Stateless Functions was introduced and lets
you write Composable components. Components that are stateless and isolated.
That means that we can write presentational components (basically components that returns good looking divs)
that are pure functions. So no state, lifecycle methods or refs attribute.
Enough said! Lets see how these Stateless Function Components can be written and used.
Typical stateful component written with ES6 syntax.
Parent passes down addText method and the state
to our presentational component Text.
Lets create our stateless function component. If you are using the ES6 way of writing function expressions
remember that the name property will not be set. Which can be a problem for debugging.
Text takes the properties as a parameter passed down from its parent Parent.
Could also pass the object props as a parameter
Notice how we dont have to use this keyword because its a function not an object.
You can still use Prop Validation on your function component
If your presentational component is small you can return your statement in one line
Stateless function components not only makes your code look cleaner, easier to test but also improves performance.
Check out the demo to see full example.
I hope you found this helpful, feel free to share your insights or comments below.
Enjoy your code.