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

redux-to-recoil

v0.8.2

Published

Access your Redux store through Recoil atoms and selectors

Downloads

553

Readme

Redux-to-Recoil

Read and write your Redux store through Recoil.

npm version build status test coverage dependencies status gzip size

Usage

atomFromRedux creates a Recoil wrapper around a location in Redux. This works like any other atom.

import { selector, useRecoilState, useRecoilValue } from 'recoil';
import { atomFromRedux } from 'redux-to-recoil';

const todosAtom = atomFromRedux('.todos'); // wraps state.todos

// Inside your component, use the atoms and selectors as normal. That's it.
const [todos, setTodos] = useRecoilState(todosAtom);

// Also: it's a normal Recoil atom, so it works in Recoil selectors
const todoCountSelector = selector({
  key: 'todoCount',
  get: ({ get }) => get(todosAtom).length,
});
const todoCount = useRecoilValue(todoCountSelector);

<SyncReduxToRecoil /> syncs state from Redux to Recoil. This is required.

import { SyncReduxToRecoil } from 'redux-to-recoil';

<Provider store={store}>
  <RecoilRoot>
    <SyncReduxToRecoil />
    <MyApp />
  </RecoilRoot>
</Provider>;

selectorFromReselect creates a Recoil selector from a plain selector, using Reselect or any other selector library.

import { selectorFromReselect, useRecoilValue } from 'recoil';

const todosSelector = selectorFromReselect((state) => state.todos);

// Inside your component, use the selector as normal. That's it.
const [todos, setTodos] = useRecoilState(todosAtom);

// Also: it's a normal Recoil selector, so it works in other Recoil selectors
const todoCountSelector = selector({
  key: 'todoCount',
  get: ({ get }) => get(todosSelector).length,
});
const todoCount = useRecoilValue(todoCountSelector);

If you want to dispatch changes from Recoil back to Redux then wrap your reducer with syncChangesFromRecoil and enable the writeEnabled option. This is only needed if you set Recoil values directly.

import { syncChangesFromRecoil } from 'redux-to-recoil';

// This will enable write-from-recoil (when options.writeEnabled is turned on)
const reducer = syncChangesFromRecoil(yourRootReducer);
const store = createStore(reducer);
//  Recoil atoms and writeable selectors work like normal
const todosAtom = atomFromRedux('.todos');

const [todos, setTodos] = useRecoilState(todosAtom);

setTodos(newTodoList);

Do I need this?

You probably don't need this. Redux and Recoil work fine side-by-side. You can already use values from Redux and Recoil together in a component.

This library is useful for accessing Redux state from within a Recoil selector -- which lets you call selectors conditionally, or within loops. useSelector can't do that. (Dynamic-Selectors can, though.)

It can also facilitate a migration from Redux to Recoil.

Options

Options are available to control how and whether Recoil receives updates from, and writes updates to, Redux.

Demo

A Todo List demo shows both a read-only sync from redux and a read-write sync.

Recoil version compatibility

| Recoil | Redux-to-Recoil | | -------: | --------------: | | 0.5.x | 0.7.1-0.8.x | | 0.4.x | 0.6.0-0.7.1 | | 0.3.x | 0.5.1-0.6.0 | | 0.2.x | 0.4.1-0.5.1 | | 0.1.x | 0.4.1 | | 0.0.13 | 0.3.1 | | 0.0.10 | 0.2.2 |

Other versions of Recoil and Redux-to-Recoil may be compatible: this table just lists the thoroughly tested pairings.