Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-use-state-reducer

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-use-state-reducer

Use fetch with hooks

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

React-Use-State-Reducer

A hook to avoid multiple declaration of const [xLoading, setXLoading] = useState(false)

Sample

npm i react-use-state-reducer

import React from "react";
import useStateReducer from "react-use-state-reducer";

export default function App() {
  const [state, dispatch] = useStateReducer({
    init: true,
    fetch: false
  });

  React.useEffect(() => {
    // /!\ Don't do that at home !! `setTimeout` had issues with React/hooks : https://medium.com/javascript-in-plain-english/usetimeout-react-hook-3cc58b94af1f
    setTimeout(() => dispatch(false, "init"), 1000);
  }, [dispatch]);

  if (state["init"]) {
    return <span>Initializing</span>;
  }

  return (
    <div>
      <span>{state["fetch"] ? "fetching" : "fetched"}</span>
      <span>{state[undefined]}</span>
      <button onClick={() => dispatch(true, "fetch")}>Start fetch</button>
      <button onClick={() => dispatch(false, "fetch")}>Stop fetch</button>
    </div>
  );
}


ReactDOM.render(<App />, document.getElementById("root"));

Keywords

FAQs

Package last updated on 25 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc