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

react-duck

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-duck

🦆 React ducks without Redux

  • 0.2.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
decreased by-40%
Maintainers
1
Weekly downloads
 
Created
Source

React Duck

NPM badge Dependabot badge Dependencies Build Status Coverage Status

Implement ducks in React following the redux pattern but using React Context.

Usage

Create the ducks for each slice of application logic.

// duck/counter.js
export default createDuck({
  name: "counter",
  initialState: 0,
  reducers: {
    increment: (state) => state + 1,
  },
  actionMapping: { otherActionType: "increment" },
});

Create the root/global duck as a combination of all other ducks.

// duck/index.js
export default createRootDuck(counterDuck, otherDuck);

Create the global context.

// context.js
export default createContext(rootDuck.reducer, rootDuck.initialState);

Use the state and actions in your component.

// app.jsx
export default function App(props) {
  const { state, dispatch } = React.useContext(Context);
  const increment = React.useCallback(
    () => dispatch(counterDuck.actions.increment()),
    [dispatch]
  );
  return (
    <div>
      Count: <span>{state[counterDuck.name]}</span>
      <button onClick={increment} />
    </div>
  );
}

Note: this is equivalent to the class component described below.

// app.jsx
export default class App extends React.PureComponent {
  static contextType = Context;

  render() {
    const { state } = this.context;
    return (
      <div>
        Count: <span>{state[counterDuck.name]}</span>
        <button onClick={this.increment} />
      </div>
    );
  }

  increment = () => {
    this.context.dispatch(counterDuck.actions.increment());
  };
}

Wrap the application in the root provider to handle state changes.

// index.jsx
const rootElement = document.getElementById("root");
const Provider = createRootProvider(Context);
ReactDOM.render(
  <Provider>
    <App />
  </Provider>,
  rootElement
);

Note: createRootProvider is just a helper and can be replaced, with the functional difference highlighted below.

diff --git a/index.jsx b/index.jsx
index 0a0a0a0..1b1b1b1 100644
--- a/index.jsx
+++ b/index.jsx
const rootElement = document.getElementById("root");
-const Provider = createRootProvider(Context);
 ReactDOM.render(
-    <Provider>
+    <Provider Context={Context}>
         <App />

A side benefit to scoping the context state to the provider is allowing multiple entire apps to be run concurrently.

Example

As a proof of concept converted the sandbox app from the react-redux basic tutorial

  • With redux example
  • Without redux code

Next

  • Implement slice selectors and useSelector hook, reference
  • Implement asynchronous middleware context support, reference
  • Implement observable pattern for context value, reference

Suggestions

  • Use immer to create immutable reducers, see guide.

Keywords

FAQs

Package last updated on 09 Sep 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