Skip to main content

Update externally

Intro

Ripples are made to be used inside a React application. However, you can also update the ripples externally.

The purpose of Ripple is to not make any compromise between the pattern you want to apply to your application.

Example:

/*
This define the Lake where the ripples will belong to.
*/
import { createRipples } from "@m-c2/ripple";

const lake = createRipples({
counter : { count: 0}
});

// We export the hooks for component usage only
export const rippleHooks = lake[0];
// We export the services for external usage
export const rippleServices = lake[1];

See it in action

Counter: 0


The updater

Let's study the updater service:

// we import the services
import { rippleServices } from "./Lake";
// we destructure the services to extract the counter updater
const { updateCounter } = rippleServices;
FunctionParameter typevalueDescriptionTrigger a render
updateCountervoidundefinedReturn the counter valueno
updateCounterfunctionhandler: (ripple) => void | "restore" | "reset" | typeof rippledo an action to the rippleyes / No
// updateCounter(void) => ripple
const ripple = updateCounter();

// updateCounter(handler: (ripple) => void | "restore" | "reset" | typeof ripple) => void
updateCounter(ripple => { ... });

Updater handler

Functionreturn typereturn valueDescriptionTrigger a render
handlervoidundefinedapply all pending modification to the rippleyes
handlerstringrestorecancel pending modificationsno
handlerstringresetreset the ripple to its initial stateno
handlertypeof ripplea new ripplereplace the ripple with a new value. Does not replace the initial stateyes
// apply all pending modification to the ripple
// updateCounter(handler: (ripple) => void ) => void
updateCounter(ripple => {
ripple.count++;
});

// cancel pending modifications
// updateCounter(handler: (ripple) => "restore") => void
updateCounter(ripple => "restore");

// reset the ripple to its initial state
// updateCounter(handler: (ripple) => "reset") => void
updateCounter(ripple => "reset");

// replace the ripple with a new value. Does not replace the initial state
// updateCounter(handler: (ripple) => typeof ripple) => void
updateCounter(ripple => {
return {
count: 0,
};
});