Custom Components
Creating your own components using Bolt
So far, we learned how to create native components, how to nest them and how to add properties and events to them. In this page, we will learn how to create our very own components using other components.
Components from Variables
Bolt components can be normal variables, that will store some combination of other components, which can also have custom nested components.
Let's say I have some component that shows some name of user and their age
Here I created this variable, now I can use this variable directly into other components as their children or in the render function
Now, you can also use variables inside the declaration of your custom components, like this:
This means you can have nested functions and variables and it will work just as well.
Components from function
A function that will return a bolt component is a component function. Your function can do anything inside it's body, or call any function, but it MUST return a HTMLDocument, and it must not be an async function, since async function returns Promise object.
There is no hard rule for creating a functional component in Bolt as mentioned above, so you can use any way that suites you, but make sure your function returns a Bolt component, or another functional component.
Receiving custom props in functional component
Your functional component can accept any type of props, and they can be of any type, and it's upto you how you manage them and return a valid bolt component. Let's see a simple example:
Higher Order Components
All the custom components that you make are 100% compatible with any higher order components that are provided by Bolt.
That means you can assign ids, events, classes and other things to a custom components, be it variable component or functional component, and it will have the exact same effect.
Let's see some examples:
And for functional component:
Nesting components
Now that we saw how to create custom components, with approaches like functional components and variable components, we can now get to create nested components.
We can do it other way around as well. This opens up several option for developers to create interesting patterns in their applications, unique to themselves since Bolt does not binds any strict rule except as mentioned above.
Last updated