
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-algorithm
Advanced tools
We often meet cases when we need to implement multistep scenario. Wizards, complex forms are typical cases when we think of some kind of algorithm that defines each-step-result-dependant behavour. You are completely lucky if you are using React (so am i).
react-algorithmWe often meet cases when we need to implement multistep scenario. Wizards, complex forms are typical cases when we think of some kind of algorithm that defines each-step-result-dependant behavour. You are completely lucky if you are using React (so am i). Let me introduce you to react-algorithm that will help you implement complex algorithms with ease.
$ npm install react-algorithm
or
$ yarn add react-algorithm
First, we should create context for our algorithm:
// context.ts
import { createAlgorithmContext } from 'react-algorithm';
const { AlgorithmProvider, withAlgorithm } = createAlgorithmContext();
export { AlgorithmProvider, withAlgorithm };
NOTE: If we decide to use multiple algorithms in our app, we createAlgorithmContext() for each
Before we go to interesting JSX part, let's define terms:
algorithm is the synchronous function which receives current step and result as params and returns next step(s) or null if there are no steps. It's intentionally synchronous. Consider algorithm as a skeleton of steps. Each step might be finished with different results. Result define which step(s) will be activated. All other logic and side effects are none of our business.So, let's create steps and algorithm function in separate file:
// algorithm.ts
export const steps = {
STEP1: 'STEP1',
STEP2: 'STEP2',
STEP3: 'STEP3',
}
export const algorithm = (stepToFinish: string, result: any) => {
switch (stepToFinish) {
case steps.STEP1:
return steps.STEP2;
case steps.STEP2:
return steps.STEP3;
default:
return null;
}
}
NOTE: algorithm might be not that consequent, check result to complicate the logic
react-algorithm uses React.Context under the hood and all steps should be within <AlgorithmProvider /> that we created earlier. Let's render provider and pass algorithm function as a prop:
// Container.tsx
import React from 'react';
import { steps, algorithm } from './algorithm';
import { AlgorithmProvider } from './context';
export default () => (
<AlgorithmProvider
initialStep={steps.STEP1}
algorithm={algorithm}
>
... // <-- steps are somewhere here
</AlgorithmProvider>
);
NOTE: initialStep matters at first rendering only. It may be useful for different reasons. For example, you are able to resume your algorithm from any step if initialStep is passed from behind.
Now, we are ready to create components that will be displayed during steps. HOC withAlgorithm connects a component to the algorithm through prop finishStep which is a function that receives result as a prop:
// Steps.tsx
import React from 'react';
import { withAlgorithm } from './context';
import { steps } from './algorithm';
const Foo = ({ name, finishStep }) => (
<div>
{name}
<button onClick={() => { finishStep(1); }}>Next</button>
</div>
);
export const Component1 = withAlgorithm({
name: 'Component1', // Custom prop
step: steps.STEP1 // Component will be displayed on `steps.STEP1`
})(Foo);
export const Component2 = withAlgorithm({
name: 'Component2',
step: steps.STEP2
})(Foo);
export const Component3 = withAlgorithm({
name: 'Component3',
step: steps.STEP3
})(Foo);
The resulting JSX will be like this:
// Container.tsx
import React from 'react';
import { steps, algorithm } from './algorithm';
import { AlgorithmProvider } from './context';
import { Component1, Component2, Component3 } from './Steps';
export default () => (
<AlgorithmProvider
initialStep={steps.STEP1}
algorithm={algorithm}
>
<Component1 />
<Component2 />
<Component3 />
</AlgorithmProvider>
);
FAQs
We often meet cases when we need to implement multistep scenario. Wizards, complex forms are typical cases when we think of some kind of algorithm that defines each-step-result-dependant behavour. You are completely lucky if you are using React (so am i).
We found that react-algorithm 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.