New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-state-effects

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-state-effects

React useState hook enhanced with side effects

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
121
decreased by-53.28%
Maintainers
1
Weekly downloads
 
Created
Source

React useStateEffects hook

Allows excecution of side effects immediately after state is set by setState hook.

Example

import useStateEffects from 'react-state-effects';

const Example = ({ callback, children }) => {
  const [state, setState] = useStateEffects(1);

  React.useEffect(() => {
    setState(state$ => [state$ + 1, callback]);
  }, [])

  return children;
}

setState could accept zero, one or multiple callbacks:

const cb1 = () => {...}
const cb2 = () => {...}

setState(state => [state + 1]);
setState(state => [state + 1, cb1]);
setState(state => [state + 1, cb1, cb2]);

In case current state is needed within callback, latest state could be injected by saving it within ref:

import useStateEffects from 'react-state-effects';

const Example = ({ callback, children }) => {
  const [state, setState] = useStateEffects(1);
  const stateRef = React.useRef(state);

  React.useEffect(() => {
    setState(state$ => [state$ + 1, () => callback(stateRef.current)]);
  }, [])

  React.useEffect(() => {
    stateRef.current = state;
  }, [state])

  return children;
}

FAQs

Package last updated on 21 Jun 2019

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