βοΈGetting started with Atomico for React users
Hi, I'm Atomico js and I bring you the React syntax for webcomponents, I think you and I get along very well π.
First let's say that Atomico is light since it has a size close to 3kB vs React + ReactDOM that have a size close to 60kB, now if your project is already written in React I can integrate Atomico progressively since a component created can be instantiated as a component for React thanks to @atomico/react, example:
Magical πͺ, isn't it?... well now let's speed up your Atomico learning path:
How to declare a component?
Atomico, like React, allows a declaration of components using only functions, example:
From the example we will highlight the following differences:
In Atomico you only use one import.
useProp
is likeuseState
, but with the difference that useProp references the state from the webcomponent property defined incounter.props
.counter.props
allows us to create the properties of our webcomponent, these are like React's propTypes, but with a big difference they are associated with the instance and can be read and modified by referencing the node, exampledocument.querySelector("my-counter").count = 10;
ReactDom.render
needs a reference to mount the component, in Atomico you only need to create themy-counter
tag to create a new instance of the component.The
<host/>
tag is similar to<> </>
for React, but<host/>
represents the webcomponent instance and every component created with Atomico must return the host tagThis is only readability, but in Atomico by convention we do not use capital letters when naming our component, these are only used when creating the customElement as in line 16, since
Counter
is instantiable.
Now I want to invite you to learn how to declare a style using Atomico.
How do you declare styles using Atomico?
It is common to see the use of libraries such as Emotion or styled-components to encapsulate styles in React, but these add an additional cost, be it for performance or bundle, in Atomico there is no such cost.
Instances, children and slots
It is normal for React to create components that you then instantiate within other components, for example:
with Atomico there are certain differences:
1. With Atomico you can instantiate CustomElements using its constructor
The constructor in Atomic is the product of the c function and is the one you will use to register your webcomponent, example:
According to the previous example, you can instantiate MyComponent as a JSX Component, example:
This instance type allows autocompletion at the JSX level and type validation at the Typescript level.
2. With Atomico you can instantiate components as functions as long as these are only stateless functions
This will be useful for reusing templates, but always remember stateless.
From React to AtomicoVirtualDOM api differencesLast updated