βš›
From React to Atomico
Atomico inherits part of the React syntax and applies it to webcomponents, with a closer to standard approach.

If you develop with React, you will know 80% Atomico ... now why use Atomico?:

  1. 1.
    Atomico will not limit your React learning curve, what you learned in Atomico is applicable in React, for example hooks and virtualDOM.
  2. 2.
    Atomico is 3kB in size which is 7% of React + React-dom.
  3. 3.
    Better component abstraction, for example the use of the ShadowDOM will avoid the need to use css-in-js like styles-components or emotion, reducing dependencies.
  4. 4.
    Agnostic Components, what you create with React only works within React, what you create with Atomico works on the web, so you can your components within React, Vue, Svelte or Html.
  5. 5.
    Exclusive component for React, the CLI @ atomico / exports automatically generates a wrapper component of your webcomponent for React, improving backward compatibility with React.
The following examples show some differences between React and Atomico.

Counter example

React
Atomico βœ…
1
import { useState } from "react";
2
import ReactDOM from 'react-dom'
3
​
4
function Counter({initialCount}) {
5
const [count, setCount] = useState(initialCount);
6
return (
7
<>
8
Count: {count}
9
<button onClick={() => setCount(initialCount)}>Reset</button>
10
<button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
11
<button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
12
</>
13
);
14
}
15
​
16
​
17
render(
18
<Counter initialCount={1}/>,
19
document.querySelector("#counter")
20
);
Copied!
1
import { c, useProp } from "atomico";
2
​
3
function counter() {
4
const [count, setCount] = useProp("count");
5
return (
6
<host>
7
Count: {count}
8
<button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
9
<button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
10
</host>
11
);
12
}
13
​
14
counter.props = { count: { type: Number, value: 0 } }
15
​
16
const Counter = c(counter);
17
​
18
customElements.define(
19
"my-counter",
20
Counter
21
);
Copied!
From the example we will highlight the following differences:
  1. 1.
    In Atomico you only use one import.
  2. 2.
    useProp is like useState, but with the difference that useProp references the state from the webcomponent property defined in counter.props.
  3. 3.
    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, example document.querySelector("my-counter").count = 10;
  4. 4.
    ReactDom.render needs a reference to mount the component, in Atomico you only need to create the my-counter tag to create a new instance of the component.
  5. 5.
    The <host/> tag is similar to <> </> for React, but <host/> represents the webcomponent instance and every component created with Atomico must return the host tag
  6. 6.
    This 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.

CustomHooks example

React
Atomico βœ…
1
import { useEffect, useState } from "react";
2
​
3
function useJsonPlaceholder(path) {
4
const [state, setState] = useState();
5
useEffect(() => {
6
let cancel;
7
setState(null);
8
fetch(`https://jsonplaceholder.typicode.com/${path}`)
9
.then(() => res.json())
10
.then((data) => !cancel && setState( data ));
11
return () => (cancel = true);
12
}, [path]);
13
}
14
​
15
function Component() {
16
const posts = useJsonPlaceholder("posts");
17
return (
18
<>
19
{posts
20
? posts.map(({ title }) => <h1>{title}</h1>)
21
: "Loading..."}
22
</>
23
);
24
}
Copied!
1
import { useEffect, useState } from "atomico";
2
​
3
function useJsonPlaceholder(path) {
4
const [state, setState] = useState();
5
useEffect(() => {
6
let cancel;
7
setState(null);
8
fetch(`https://jsonplaceholder.typicode.com/${path}`)
9
.then(() => res.json())
10
.then((data) => !cancel && setState( data ));
11
return () => (cancel = true);
12
}, [path]);
13
}
14
​
15
function component() {
16
const posts = useJsonPlaceholder("posts");
17
return (
18
<host>
19
{posts
20
? posts.map(({ title }) => <h1>{title}</h1>)
21
: "Loading..."}
22
</host>
23
);
24
}
Copied!
From the example we will highlight the following differences:
  1. 1.
    The hook api is the same.
  2. 2.
    the component in Atomico returns the `<host/> tag.

Supported hooks

In Atomico you will have the most useful React hooks such as:
  1. 1.
    useRef
  2. 2.
    useState
  3. 3.
    useReducer
  4. 4.
    useLayoutEffect
  5. 5.
    useEffect
  6. 6.
    useMemo
  7. 7.
    useCallback
  8. 8.
    useContext : Not supported, event api is better practice than context when using webcomponents, example useChannel​

CSS-in-JS

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.
React
Atomico βœ…
1
const Button = styled.a`
2
/* This renders the buttons above... Edit me! */
3
display: inline-block;
4
border-radius: 3px;
5
padding: 0.5rem 0;
6
margin: 0.5rem 1rem;
7
width: 11rem;
8
background: transparent;
9
color: white;
10
border: 2px solid white;
11
​
12
/* The GitHub button is a primary button
13
* edit this to target it specifically! */
14
${props => props.primary && css`
15
background: white;
16
color: black;
17
`}
18
`
19
​
20
render(
21
<div>
22
<Button
23
href="https://github.com/styled-components/styled-components"
24
target="_blank"
25
rel="noopener"
26
primary
27
>
28
GitHub
29
</Button>
30
​
31
<Button as={Link} href="/docs">
32
Documentation
33
</Button>
34
</div>
35
)
Copied!
1
import { c, css } from "atomico";
2
​
3
function button() {
4
return <host shadowDom><slot/></host>;
5
}
6
​
7
button.props = { primary: { type: Boolean, relfect: true } };
8
​
9
button.styles = css`
10
:host {
11
display: inline-block;
12
border-radius: 3px;
13
padding: 0.5rem 0;
14
margin: 0.5rem 1rem;
15
width: 11rem;
16
background: transparent;
17
color: white;
18
border: 2px solid white;
19
}
20
​
21
:host([primary]) {
22
background: white;
23
color: black;
24
}
25
`;
26
​
27
export const Button = c(button);
Copied!
Last modified 12d ago