Giter Site home page Giter Site logo

recoil-anywhere's Introduction

Recoil Anywhere

This is a tiny little addition to Recoil.js. It allows you to use Recoil anywhere in your code, not just inside React Components. It also includes a useReactRef to enable using Recoil without components re-render.

Installation

The Recoil Anywhere package lives in npm. To install, run the following command:

npm install --save recoil-anywhere

Set up

Add Recoil Anywhere custom hook inside your Recoil Root:

import React from "react";
import { RecoilRoot } from "recoil";
import { RecoilAnywhere } from "recoil-anywhere";

function App() {
  return (
    <RecoilRoot>
      <RecoilAnywhere />
      ...
    </RecoilRoot>
  );
}

Now you can use Recoil anywhere

Examples

  1. Using Recoil inside functions:
// atom.js
const CounterState = atom({
  key: "CounterState",
  default: 1,
});

// math.js
const multiply = (a, b) => a * b;

// component.tsx:
import { recoil } from "recoil-anywhere";
import { CounterState } from "./atom.js";
import { multiply } from "./math.js";

function Component() {
  function multiplyCounter(multiplier: number) {
    const counter = recoil.get(CounterState);
    return multiply(counter, multiplier);
  }
}
  1. In fact, now Recoil can be used in any file, even outside React Components:
// computed.ts
import { recoil } from 'recoil-anywhere'
import { CounterState } from './atom.js'

const getMultipliedCounter = (multiplier: number) {
    const counter = recoil.get(CounterState)
    return counter * multiplier
}

Recoil Ref Hook

This package also contains Recoil Ref hook. It is like React's useRef, but in Recoil. It prevents unwanted re-renders when Recoil value is updated:

import { useRecoilRef } from 'recoil-anywhere'
import { CounterState } from './atom.js'

function Component() {
    const counter = useRecoilRef(CounterState)
    ...
}

recoil-anywhere's People

Contributors

mmamedel avatar

Watchers

 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.