Giter Club home page Giter Club logo

react-solid-state's Introduction

React Solid State

This is a local state swap for React using Solid.js. Instead of worry about when your components should update you can use declarative data. This makes use of the new React Hooks API. However it differs in a few really key ways:

  • Dependencies are automatically tracked. While there is an option to set explicit dependencies it is isn't necessary.
  • Nested hooks are allowed. Effects that produce sub nested effects are fair game.

The goal here is to give as close as possible to Solid's easy state management and fine grained dependency detection while still being able to use React. All of Solid's API methods have been ported. Note: this uses Hooks so it only works with Function Components which is consistent with how Components work in Solid.

There are a few differences in the API from some same named Hooks from React. Solid State are objects much like traditional React State. There is a useCleanup method that lets you register release code at both the component unmount level and in each Hook. useEffect doesn't expect a cleanup/dispose method returned for that reason. useMemo (and useSignal) return getters rather than the the pure value. This is because the context under data is accessed is the key to automatic dependency tracking. For all the information of how Solid works look at the Documentation.

To get started simply wrap your components withSolid HOC. From there use your hooks.

import { withSolid, useState } from 'react-solid-state'
import React from 'react'

const WelcomeComponent = withSolid(props => {
  const [state, setState] = useState({ recipient: 'John' });
  return <div onClick={() => setState({ recipient: 'Jake' })}>
    Hello { this.state.recipient }
  </div>
})
import { withSolid, useState, useEffect, useCleanup } from 'react-solid-state'
import React from 'react'

const CounterComponent = withSolid(props => {
  const [state, setState] = useState({ count: 0 });
  useEffect(() => {
    const timer = setInterval(() => setState('count', c => c + 1), 1000);
    useCleanup(() => clearInterval(timer));
  })
  return <div>{state.count}</div>
})

react-solid-state's People

Contributors

ryansolid avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.