Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 16, 2020 09:40 am GMT

Using CSS-in-JS with Preact

The two most popular CSS-in-JS libraries, styled-components and Emotion, dont support Preact out of the box. However there is an easy workaround.

Add these aliases to your webpack.config.js file:

module.exports = {    resolve: {        alias: {            react: 'preact/compat',            'react-dom/test-utils': 'preact/test-utils',            'react-dom': 'preact/compat',        },    },};

Adding these aliases allows you to use other libraries intended for React too!

And if youre using Storybook, youll need to add the same alias to your .storybook/main.js file:

module.exports = {    stories: ['../app/javascript/**/story.tsx'],    webpackFinal: async config => {        config.resolve.alias = Object.assign({}, config.resolve.alias, {            react: "preact/compat",            'react-dom': 'preact/compat'        })        return config;      },};

After this you can install your preferred package:

npm i @emotion/styled# ORnpm i styled-components

And use either styled-components or Emotion without any problems:

import styled from '@emotion/styled'; // ORimport styled from 'styled-components';export const Container = styled.div`  background-color: pink;`;

Original Link: https://dev.to/emma/using-css-in-js-with-preact-30ko

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To