npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reactify-svelte

v1.1.0

Published

A React component for using Svelte components in React applications.

Downloads

44

Readme

reactify-svelte

reactify-svelte is am NPM that provides a simple and convenient way to use Svelte components in React applications. It is a wrapper around the svelte package that provides a React components to mount Svelte components in React applications.

Installation

To install reactify-svelte, use the following command:

# npm
npm install --save reactify-svelte

# yarn
yarn add reactify-svelte

# pnpm
pnpm add reactify-svelte

Usage

Prerequisites

Before using this package, make sure you have the following prerequisites:

  • Create React App with Vite
# npm
npm init vite@latest my-react --template react
npm init vite@latest my-react --template react-ts

# yarn
yarn create vite my-react --template react
yarn create vite my-react --template react-ts

# pnpm
pnpx create-vite my-react --template react
pnpx create-vite my-react --template react-ts
  • Install Svelte
# npm
npm install --save svelte

# yarn
yarn add svelte

# pnpm
pnpm add svelte
  • Install Sveltejs plugin for Vite
# npm
npm install --save-dev @sveltejs/vite-plugin-svelte

# yarn
yarn add --dev @sveltejs/vite-plugin-svelte

# pnpm
pnpm add --dev @sveltejs/vite-plugin-svelte
  • Install reactify-svelte
# npm
npm install --save reactify-svelte

# yarn
yarn add reactify-svelte

# pnpm
pnpm add reactify-svelte

Note: If you are using TypeScript, you need to add the following to the src/svelte-components.d.ts file.

// add the following to the svelte-components.d.ts file
declare module '*.svelte' {
  import { SvelteComponent } from 'svelte';

  const value: SvelteComponent;
  export default value;
}

Configuration

To use reactify-svelte, you need to configure the following:

  • Add the @sveltejs/vite-plugin-svelte plugin to the vite.config.(js|ts) file.
// add the following to the vite.config.(js|ts) file
import { svelte } from '@sveltejs/vite-plugin-svelte';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), svelte()],
});

Example

Create a Svelte component file ./components/Hello.svelte with the following content:

<script lang="ts">
  export let txt = "I'm Svelte!";
  export let counter = 0;
</script>

<div>
  <h1>{txt}</h1>
  <p>Counter: {counter}</p>
</div>

<style>
  div {
    background-color: #8EC5FC;
    background-image: linear-gradient(62deg, #8EC5FC 0%, #E0C3FC 100%);
    padding: 2rem;
  }

  h1 {
    color: #fff;
    text-align: center;
    font-size: 4rem;
  }

  p {
    text-align: center;
    font-size: 1.2rem;
    color: #333;
  }
</style>

Then, create a React component file App.tsx with the following content:

import { useState } from 'react';
import { SvelteWrapper } from 'reactify-svelte';
import Hello__SvelteComponent__ from './components/Hello.svelte';

interface SvelteProps {
  txt: string;
  counter: number;
}

const HelloSvelteComponent = SvelteWrapper<SvelteProps>(
  Hello__SvelteComponent__,
);

const App = () => {
  const [counter, setCounter] = useState(0);
  return (
    <>
      <HelloSvelteComponent txt="Hello Svelte from React!" counter={counter} />
      <button type="button" onClick={() => setCounter(counter + 1)}>
        Increment
      </button>
      <button type="button" onClick={() => setCounter(counter - 1)}>
        Decrement
      </button>
      <button type="button" onClick={() => setCounter(0)}>
        Reset
      </button>
    </>
  );
};

export default App;

Note: If you are using TypeScript, you need to add the following to the src/svelte-components.d.ts file.

// add the following to the svelte-components.d.ts file
declare module '*.svelte' {
  import { SvelteComponent } from 'svelte';

  const value: SvelteComponent;
  export default value;
}

API

SvelteWrapper

This is a higher-order component (HOC) provided by reactify-svelte that wraps the Svelte component and makes it usable in React applications. It takes the Svelte component as an argument and returns a new React component.

License

This package is licensed under the MIT License.