GithubHelp home page GithubHelp logo

andarist / react-selector-hooks Goto Github PK

View Code? Open in Web Editor NEW
85.0 4.0 0.0 5 KB

Collection of hook-based memoized selector factories for declarations outside of render.

JavaScript 100.00%
react reactjs hooks hook memoize memoization reselect

react-selector-hooks's Introduction

react-selector-hooks

Collection of hook-based memoized selector factories for declarations outside of render.

Motivation

Reusing existing functions. It also might often be desirable to declare selector functions outside of render to keep render functions less bloated.

Instead of this:

function Component({ a, b }) {
  const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b])
  return <span>{memoizedValue}</span>
}

You can write this:

const useSelector = createSelector(computeExpensiveValue)

function Component({ a, b }) {
  const memoizedValue = useSelector(a, b)
  return <span>{memoizedValue}</span>
}

API

createSelector(resultFunc)

import * as React from 'react'
import { createSelector } from 'react-selector-hooks'

const useSelector = createSelector(computeExpensiveValue)

export default function Component({ a, b }) {
  const memoizedValue = useSelector(a, b)
  return <span>{memoizedValue}</span>
}

createStateSelector([inputSelectors], resultFunc)

This is really similar to reselect's createSelector.

import * as React from 'react'
import { createStateSelector } from 'react-selector-hooks'

const useSelector = createStateSelector(
  [
    state => state.values.value1,
    (state, a) => state.values.value2 + a,
    (state, a) => state.values.value3 * a,
  ],
  (value1, value2, value3) => value1 + value2,
)

export default function Component({ a }) {
  const state = React.useContext(StoreContext)
  const memoizedValue = useSelector(state, a)
  return <span>{memoizedValue}</span>
}

createStructuredSelector({...inputSelectors}, resultFunc)

This is really similar to reselect's createStructuredSelector.

import * as React from 'react'
import { createStructuredSelector } from 'react-selector-hooks'

const useSelector = createStructuredSelector(
  {
    value1: state => state.values.value1,
    value2: (state, a) => state.values.value2 + a,
  },
  ({ value1, value2 }) => value1 + value2,
)

export default function Component({ a }) {
  const state = React.useContext(StoreContext)
  const memoizedValue = useSelector(state, a)
  return <span>{memoizedValue}</span>
}

react-selector-hooks's People

Contributors

andarist avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-selector-hooks's Issues

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.