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

react-redux-getters

v4.0.1

Published

Getters for React and Redux

Downloads

36

Readme

react-redux-getters

npm

Provides an extra layer of 'getters' between your React components and the Redux store. The getter returns the data from the store, if it's there, otherwise it returns a stub and invokes the fetch action. So the store is filled automatically.

Installation

yarn add react-redux-getters

Usage

Create getter

import { createGetter } from 'react-redux-getters'
import { updateSubjects, fetchSubjects } from 'actions/subjects'

export const getSubjects = createGetter({
  stateSelector: (state) => state.subjects,
  asyncFetcher: () => fetchSubjects(),
  stateUpdater: (data, dispatch) => dispatch(updateSubjects(data)),
})

Configure store

import { createStore, applyMiddleware } from 'redux'
import { gettersMiddleware } from 'react-redux-getters'

const store = createStore(
  reducers,
  applyMiddleware(gettersMiddleware, ...),
)

Component

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connectGetters } from 'react-redux-getters'
import { getSubjects } from 'selectors/subjects'

class Subjects extends Component {
  static propTypes = {
    subjectsGetter: PropTypes.object.isRequired
  }

  render() {
    const { subjectsGetter } = this.props
    const { isSucceded, isPending, isFailure, error, data } = subjectsGetter
    
    if (isPending) {
      return (
        <LoadingRenderer />
      )
    }

    if (isFailure) {
      return (
        <ErrorRenderer error={error} />
      )
    }

    return (
      <DataRenderer data={data} />
    )
  }
}

const mapGettersToProps = (state) => ({
  subjectsGetter: getSubjects(state),
})

export default connectGetters(mapGettersToProps)(Subjects)

Done!

Your React component will be rerendered when the getter fetches the data and stores it in the Redux store.

Usage with reselect

Create selector with getter

import { createSelector } from 'reselect'
import { createGetter, composeGetters } from 'react-redux-getters'
import { getSubjects } from 'selectors/subjects'
import { getTeachers } from 'selectors/teachers'

export const getHumanitarianSubjects = createSelector(
  getSubjects,
  (subjectsGetter) => composeGetters(
    subjectsGetter,
    (subjects) => filterHumanitarianSubjects(subjects)
  )
)

Component (usage is the same as with a regular getter)

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connectGetters } from 'react-redux-getters'
import { getHumanitarianSubjects } from 'selectors/subjects'

class HumanitarianSubjects extends Component {
  static propTypes = {
    humanitarianSubjectsGetter: PropTypes.object.isRequired
  }

  render() {
    const { humanitarianSubjectsGetter } = this.props
    const { isSucceded, isPending, isFailure, error, data } = humanitarianSubjectsGetter
    ...
  }
}

const mapGettersToProps = (state) => ({
  humanitarianSubjectsGetter: getHumanitarianSubjects(state),
})

export default connectGetters(mapGettersToProps)(HumanitarianSubjects)

More examples

Composing getters

import { createSelector } from 'reselect'
import { createGetter, composeGetters } from 'react-redux-getters'
import { getSubjects } from 'selectors/subjects'
import { getTeachers } from 'selectors/teachers'

export const getSubjectsAndTeachers = createSelector(
  getSubjects,
  getTeachers,
  (subjectsGetter, teachersGetter) => composeGetters(
    subjectsGetter,
    teachersGetter,
    (subjects, teachers) => ({ subjects, teachers })
  )
)

Using props

// Getter
import { createGetter } from 'react-redux-getters'
import { fetchTeacher, updateTeacher } from 'actions/teachers'

export const getTeacher = createGetter({
  stateSelector: (state, props) => state.teachers[props.teacherId],
  asyncFetcher: (dispatch, state, props) => fetchTeacher(props.teacherId),
  stateUpdater: (data, dispatch, state, props) => dispatch(updateTeacher(props.teacherId, data))
})

// Component
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connectGetters } from 'react-redux-getters'
import { getTeacher } from 'selectors/teachers'

class Teacher extends Component {
  ...
}

const mapGettersToProps = (state, props) => ({
  teacherGetter: getTeacher(state, { teacherId: props.teacherId }),
})

export default connectGetters(mapGettersToProps)(Teacher)

// Using component
<Teacher teacherId={1} />

Composing getters without reselect

import { composeGetters } from 'react-redux-getters'
import { getSubjects } from 'actions/subjects'

export const getSubject = (state, props) => composeGetters(
  getSubjects(state),
  (subjects) => findSubjectById(subjects, props.subjectId)
)

API

import { createGetter, composeGetters } from 'react-redux-getters'

createGetter({
  stateSelector: (state, props) => dataFromState, // Required func – should return data from store state
  asyncFetcher: async (dispatch, state, props) => fetchedData, // Required async func – should return fetched data (Promise)
  stateUpdater: (data, dispatch, state, props) => {}, // Required func - should dispatch store update
  shouldFetch: (dataFromState, state, props) => isNil(dataFromState) // Optional func – condition that fetching is needed
})

composeGetters(
  ...getters, 
  composeData: (...gettersData) => newComposedData // Func – creates composed data from incoming getters data
)

License

MIT