All Projects β†’ pedronasser β†’ resynced

pedronasser / resynced

Licence: MIT license
An experimental hook that lets you have multiple components using multiple synced states using no context provider

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to resynced

use-query-string
πŸ†™ A React hook that serializes state into the URL query string
Stars: ✭ 50 (+163.16%)
Mutual labels:  hooks, state-management, react-hooks
react-cool-form
😎 πŸ“‹ React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+1194.74%)
Mutual labels:  hooks, state-management, react-hooks
Constate
React Context + State
Stars: ✭ 3,519 (+18421.05%)
Mutual labels:  hooks, state-management, react-hooks
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (+242.11%)
Mutual labels:  hooks, state-management, react-hooks
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-31.58%)
Mutual labels:  hooks, state-management, react-hooks
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+184.21%)
Mutual labels:  hooks, state-management, react-hooks
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+23715.79%)
Mutual labels:  hooks, state-management, react-hooks
Reto
Flexible and efficient React Store with hooks.
Stars: ✭ 194 (+921.05%)
Mutual labels:  hooks, state-management
Reusable
Reusable is a library for state management using React hooks
Stars: ✭ 207 (+989.47%)
Mutual labels:  hooks, state-management
react-immer
No nonsense state management with Immer and React hooks
Stars: ✭ 13 (-31.58%)
Mutual labels:  state-management, react-hooks
react-ui-hooks
🧩Simple repository of React hooks for building UI components
Stars: ✭ 20 (+5.26%)
Mutual labels:  hooks, react-hooks
Fre
πŸ‘» Tiny Footprint Concurrent UI library for Fiber.
Stars: ✭ 3,195 (+16715.79%)
Mutual labels:  hooks, react-hooks
Haunted
React's Hooks API implemented for web components πŸ‘»
Stars: ✭ 2,197 (+11463.16%)
Mutual labels:  hooks, react-hooks
react-mlyn
react bindings to mlyn
Stars: ✭ 19 (+0%)
Mutual labels:  state-management, react-hooks
dobux
πŸƒ Lightweight responsive state management solution.
Stars: ✭ 75 (+294.74%)
Mutual labels:  hooks, state-management
rex-state
Convert hooks into shared states between React components
Stars: ✭ 32 (+68.42%)
Mutual labels:  state-management, react-hooks
React Form
βš›οΈ Hooks for managing form state and validation in React
Stars: ✭ 2,270 (+11847.37%)
Mutual labels:  hooks, react-hooks
niue
A tiny shared state and event library for React
Stars: ✭ 17 (-10.53%)
Mutual labels:  state-management, react-hooks
useSharedState
useSharedState is a simple hook that can be used to share state between multiple React components.
Stars: ✭ 0 (-100%)
Mutual labels:  hooks, state-management
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (+105.26%)
Mutual labels:  hooks, react-hooks

Resynced (experimental)

❀️ Motivation

This is an experimental hook that lets you have multiple components using multiple synced states using no context provider.

And also provide a way of using that synced state with Redux!

This package requires the Hooks API available only in React 16.7.0-alpha.0 or later.

πŸ’» Example

πŸ”§ Install

$ yarn add resynced

πŸš€ How to Use

import { createSynced } from 'resynced'

const initialState = "John"
const [useSyncedState] = createSynced(initialState)

const UsingSharedState = () => {
  const [name, setName] = useSyncedState()

  return (
    <div>
      <h1>My name is {name}</h1>
      <button onClick={() => setName("Jorge")}>Change Name</button>
    </div>
  )
}

Using with Redux

Let's first setup our synced redux store (you must have redux installed in your project)

import { createSyncedRedux } from "resynced"
import { createStore } from "redux"

const initialState = {
  authenticated: false
}

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case "LOGIN":
      return { ...state, authenticated: true }
    case "LOGOUT":
      return { ...state, authenticated: false }
    default:
      return state
  }
}

const authStore = createStore(reducer)
const [useSyncedAuth] = createSyncedRedux(authStore)

export default useSyncedAuth

Now we can use that synced redux in any component without the need of adding a Context Provider anywhere.

import React from 'react'
import useAuth from './authStore'

const ComponentUsingAuth = React.memo(() => {
  // This component will only update when the 'authenticated'
  // property is updated
  const [{ authenticated }, dispatch] = useAuth(["authenticated"])

  return (
    <div>
      <h1>Authenticated? {authenticated ? "Yes" : "No"}</h1>
      <button onClick={() => dispatch({ type: "LOGIN" })}>Login</button>
      <button onClick={() => dispatch({ type: "LOGOUT" })}>Logout</button>
    </div>
  )
})  

export ComponentUsingAuth

You can check this working example here: Resynced With Redux

Conditional updates

Using a list of properties

The component local state will only be synced if any of the given properties of the state object changes (only works with object states).

import { createSynced }  from 'resynced'

const initialState = {
  name: "John"
}
const [useSyncedState] = createSynced(initialState)

const UsingSharedState = () => {
  const [state, setState] = useSyncedState(["name"])

  return (
    <div>
      <h1>My name is {name}</h1>
      <button onClick={() => setState({ name: "Jorge" })}>Change Name</button>
    </div>
  )
}

Using functions

The component local state will only be synced if the return of the given function is true.

import { createSynced } from 'resynced'

const [useSyncedState] = createSynced("John")

const UsingSharedState = () => {
  const [name, setName] = useSyncedState((newState, prevState) => {
    return newState !== "Foo"
  })

  return (
    <div>
      <h1>My name is {name}</h1>
      <button onClick={() => setName("Jorge")}>Change Name</button>
    </div>
  )
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].