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-thunk-data

v1.22.7

Published

A lib for fetching normalized data in a redux store through thunks.

Downloads

89

Readme

A lib for fetching normalized data in a redux store through thunks.

Inspiration was taken from redux advices with async actions. A list of other frameworks like this could be found here. Also, see this post for a presentation based on the pass culture project.

CircleCI npm version

Basic Usage

You need to install a redux-thunk setup with the dataReducer from fetch-normalize-data. You can also use the requestsReducer to have a status state of the request :

import {
  applyMiddleware,
  combineReducers,
  createStore
} from 'redux'
import thunk from 'redux-thunk'
import { createDataReducer, createRequestsReducer } from 'redux-thunk-data'

const storeEnhancer = applyMiddleware(
  thunk.withExtraArgument({ rootUrl: "https://momarx.com" })
)
const rootReducer = combineReducers({
  data: createDataReducer({ foos: [] }),
  requests: createRequestsReducer()
})
const store = createStore(rootReducer, storeEnhancer)

Then you can request data from your api that will be stored in the state.data

react old school

import React, { PureComponent } from 'react'
import { requestData } from 'redux-thunk-data'


class Foos extends PureComponent {
  constructor () {
    super()
    this.state = { error: null }
  }

  handleFooClick = foo => () => {
    const { dispatch } = this.props
    dispatch(requestData({
      apiPath: '/foos',
      body: {
        isOkay: !foo.isOkay
      },
      method: 'PUT'
      handleFail: (state, action) =>
        this.setState({ error: action.payload.error })
    }))
  }

  componentDidMount () {
    const { dispatch } = this.props
    dispatch(requestData({
      apiPath: '/foos',
      handleFail: (state, action) =>
        this.setState({ error: action.payload.error })
    }))
  }

  render () {
    const { foos, isFoosPending } = this.props
    const { error } = this.state

    if (isFoosPending) {
      return 'Loading foos...'
    }

    if (error) {
      return error
    }

    return (
      <>
        {(foos || []).map(foo => (
          <button
            key={foo.id}
            onClick={this.handleFooClick(foo)}
            type="button"
          >
            {foo.isOkay}
          </button>
        ))}
      </>
    )
  }
}

const mapStateToProps = state => ({
  foos: state.data.foos,
  isFoosPending: (state.requests.foos || {}).isPending
})
export default connect(mapStateToProps)(Foos)

NOTE: We could also used a handleSuccess in the requestData api, in order to grab the action.data foos. In that case, code to be modified is:

constructor () {
  this.state = { error: null, foos: [] }
}

handleFooClick = foo => () => {
  const { dispatch } = this.props
  dispatch(requestData({
    apiPath: '/foos',
    body: {
      isOkay: !foo.isOkay
    },
    method: 'PUT'
    handleFail: (state, action) =>
      this.setState({ error: action.error })
    handleSuccess: (state, action) => {
      const { foos } = this.props
      const nextFoos = foos.map(foo => {
        if (foo.id === action.payload.datum.id) {
          return {...foo, action.payload.datum }
        }
        return foo
      })
      this.setState({ foos: nextFoos })
    },
  }))
}

componentDidMount () {
  const { dispatch } = this.props
  dispatch(requestData({
    apiPath: '/foos',
    handleFail: (state, action) => this.setState({ error: action.payload.error }),
    handleSuccess: (state, action) => this.setState({ foos: action.payload.data }),
    method:'GET'
  }))
}

render () {
  const { error, foos } = this.state
  ...
}

But if your rendered foos array should be coming from a memoizing merging (and potentially normalized) (and potentially selected from inter data filter conditions) state of foos, then syntax goes easier if you pick from the connected redux store lake of data.

react hooks school

import React, { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { requestData } from 'redux-thunk-data'

const Foos = () => {
  const dispatch = useDispatch()

  const [error, setError] = useState(null)


  const foos = useSelector(state =>
    state.data.foos)

  const { isPending: isFoosPending } = useSelector(state =>
    state.requests.foos) || {}


  const handleFooClick = foo => () =>
    dispatch(requestData({
      apiPath: '/foos',
      body: { isOkay: !foo.isOkay },
      method: 'PUT'
      handleFail: (state, action) => setError(action.payload.error)
    }))


  useEffect(() =>
    dispatch(requestData({
      apiPath: '/foos',
      handleFail: (state, action) => setError(action.payload.error)
    })), [dispatch])


  if (isFoosPending) {
    return 'Loading foos...'
  }

  if (error) {
    return error
  }

  return (
    <>
      {(foos || []).map(foo => (
        <button
          key={foo.id}
          onClick={handleFooClick(foo)}
          type="button"
        >
          {foo.isOkay}
        </button>
      ))}
    </>
  )
}