![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
react-duck
Advanced tools
Implement ducks in React following the redux pattern but using React Context.
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.
As a proof of concept converted the sandbox app from the react-redux basic tutorial
useSelector
hook, referenceimmer
to create immutable reducers, see guide.FAQs
🦆 React ducks without Redux
The npm package react-duck receives a total of 5 weekly downloads. As such, react-duck popularity was classified as not popular.
We found that react-duck demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.