Solid Component in React App using Web Components

I wanted to use a Solid element inside a React app. In the end, I was pleasantly surprised how smooth everything went.

This is a quick guide that highlights important steps.

Advantages

You can use the same component everywhere, even witho…


This content originally appeared on DEV Community and was authored by Fatih Pense

I wanted to use a Solid element inside a React app. In the end, I was pleasantly surprised how smooth everything went.

This is a quick guide that highlights important steps.

Advantages

  • You can use the same component everywhere, even without frameworks.
  • Output size is very small and doesn't contain a big runtime.
  • All the good stuff Solid brings.

Scope

Using React component in Solid, or having children React components in this Custom Component are hard problems I won't mention.

End Result

Resources

solid-element library:
https://github.com/solidjs/solid/tree/main/packages/solid-element

It is easier to have some understanding before diving in:
https://developer.mozilla.org/en-US/docs/Web/Web_Components

Best practices:
https://developers.google.com/web/fundamentals/web-components/best-practices
"Aim to only accept rich data (objects, arrays) as properties."

Steps

1- Start with the template
npx degit solidjs/templates/ts my-app

2- Add dependencies
pnpm i solid-element

3- Change vite.config.ts

import { defineConfig } from "vite";
import solidPlugin from "vite-plugin-solid";

const path = require('path')

export default defineConfig({
  plugins: [solidPlugin()],
  build: {
    target: "esnext",
    polyfillDynamicImport: false,
    lib: {
      entry: path.resolve(__dirname, 'src/MyComponent.tsx'),
      name: 'MyLib'
    },
  },
});

4- Create Component MyComponent.tsx

import { onMount, createSignal, createEffect, For } from "solid-js";
import { createStore } from "solid-js/store";

import { customElement } from "solid-element";

const [getData, setData] = createSignal("");

interface Options {
  option1: string;
  option2: number;
}

customElement(
  "my-custom-component",
  {
    data: { getData, setData, getOtherData: null },
  },
  (
    props: {
      data: {
        // flowdata: string;
        getData: () => string;
        setData: (v: string) => string;
        getOtherData: (options: Options) => Promise<string>;
      };
    },
    { element }
  ) => {
    let internal_el;

    props.data.getOtherData = async (
      options: Options = { option1: "default", option2: 1 }
    ): Promise<string> => {
      let promise = new Promise<string>((resolve, reject) => {
        //do something
        resolve("data");
      });
      return promise;
    };

    const [state, setState] = createStore({});

    onMount(() => {
      // code
    });

    createEffect(() => {
      // getData() will be reactive here
      // you can use the passed data to do calculation / render elements
      getData();
    });

    return <div ref={internal_el}></div>;
  }
);


5- Change package.json name field:
"name": "my-custom-component"

6- Run npm run build
Now you can see the result under dist directory. That is all. You can copy my-custom-component.es.js to your React project, or use some multi-repo setup.

7- On the React side of things, you can use methods to exchange data with the Custom Component.


import "../vendor/my-custom-component.es.js";

function Component1(props) {
  const customControlRef = useRef<any>(null);

  useEffect(() => {
    customControlRef.current.data.setData(specialData);
  }, []);

  const getData2 = async (ev) => {
    await customControlRef.current.data.getOtherData();
  };

  return (
    <div>
      <my-custom-component ref={customControlRef}></my-custom-component>

      <button className="button" onClick={getData2}>
        Get some data from Custom Component
      </button>
    </div>
  );
}


This content originally appeared on DEV Community and was authored by Fatih Pense


Print Share Comment Cite Upload Translate Updates
APA

Fatih Pense | Sciencx (2021-07-10T16:51:39+00:00) Solid Component in React App using Web Components. Retrieved from https://www.scien.cx/2021/07/10/solid-component-in-react-app-using-web-components/

MLA
" » Solid Component in React App using Web Components." Fatih Pense | Sciencx - Saturday July 10, 2021, https://www.scien.cx/2021/07/10/solid-component-in-react-app-using-web-components/
HARVARD
Fatih Pense | Sciencx Saturday July 10, 2021 » Solid Component in React App using Web Components., viewed ,<https://www.scien.cx/2021/07/10/solid-component-in-react-app-using-web-components/>
VANCOUVER
Fatih Pense | Sciencx - » Solid Component in React App using Web Components. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/10/solid-component-in-react-app-using-web-components/
CHICAGO
" » Solid Component in React App using Web Components." Fatih Pense | Sciencx - Accessed . https://www.scien.cx/2021/07/10/solid-component-in-react-app-using-web-components/
IEEE
" » Solid Component in React App using Web Components." Fatih Pense | Sciencx [Online]. Available: https://www.scien.cx/2021/07/10/solid-component-in-react-app-using-web-components/. [Accessed: ]
rf:citation
» Solid Component in React App using Web Components | Fatih Pense | Sciencx | https://www.scien.cx/2021/07/10/solid-component-in-react-app-using-web-components/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.