Tips

Here are some tips you can take into account when creating webcomponents with Atomico

Component name as function

Write the functional component using the first lowercase character, since the functional declaration is not instantiable as a constructor in JSX.

function component() {
  return <host />;
}

This prevents confusion when identifying the constructor of the component instance. Atomico if it supports instances as Constructors, see cases.​

useProp

preferably use useProp in the following cases:

  • By modifying the prop from inside the component.

function useCounter(prop) {
  const [value, setValue] = useProp(prop);
  return {
    value,
    increment() {
      setValue(value + 1);
    },
  };
}
  • By isolating the logic of the prop in a customHook.

function useCounter(prop) {
  const [value, setValue] = useProp(prop);
  return {
    value,
    increment() {
      setValue(value + 1);
    },
  };
}

In most cases downloading the prop from the first argument of the function is simpler and more declarative, example:

function component({ value }) {
  return <host>{value}</host>;
}

component.props = {
  value: Number,
};

Atomico has type support in both JSDOC and Typescript by inferring the types of the props.​

Prefers the use of static styles

function component() {
  return <host shadowDom />;
}

component.styles = css`
  :host {
    display: block;
  }
`;

This does not rule out the use within the style tag, since it is sometimes the solution to the definition of conditional styles or variables to the logic and outside the scope of the custom properties.

Last updated