Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
compose-state
Advanced tools
compose-state
is a library to compose multiple state updaters in React.
compose-state
works with the standard setState
parameters – objects or functions – so you don’t have to learn any new syntax. It’s also compatible with React’s new getDerivedStateFromProps
lifecycle method.
yarn add compose-state
or npm install compose-state
import composeState from 'compose-state';
// or
import {
composeState,
composeDerivedStateFromProps,
} from 'compose-state';
Let's say you want to call setState
and do two things
Both of these updaters need to be functional, since they rely on the previous state for their return values.
const updateScore = s => ({ score: s.score + 1 });
const logTime = s => ({ log: [...s.log, Date.now()] });
Normally, we would need to call setState
for both of these functions
class Game extends Component {
onScore = () => {
this.setState(updateScore);
this.setState(logTime);
};
// ...
}
...or we rewrite the two updaters into one larger function.
const updateScoreLogTime = s => ({
score: s.score + 1,
log: [...s.log, Date.now()],
});
But with compose-state
, we can keep these two updaters independent, and we won't have to bulk up our component code with more setState
calls.
const updateScoreLogTime = composeState(updateScore, logTime);
class Game extends Component {
onScore = () => {
this.setState(updateScoreLogTime);
};
// ...
}
compose-state
isn't dependent on React at all, it's just a big reducer function. This allows you to build and compose as many updaters as you want while keeping your actual component code simple and maintainable.
compose-state
accepts both objects and functions, just like setState
. This allows you to mix static and dynamic updaters without increasing the complexity of any individual parameter.
const defaultValue = { value: 0 };
const incrementOther = s => ({ other: s.other + 1 });
this.setState(
composeState(defaultValue, incrementOther)
);
compose-state
comes with a composeDerivedStateFromProps
function to use with React's new getDerivedStateFromProps
lifecycle method.
const updater1 = (nextProps, prevState) => {
// ...
}
const updater2 = (nextProps, prevState) => {
// ...
}
class App extends Component {
static getDerivedStateFromProps = composeDerivedStateFromProps(
updater1, updater2
)
// ...
}
While more formal state managers push developers away from controlling state in React, compose-state
simply enhances state control methods that are primitive to the platform.
compose-state
is a lot like Classnames. It's a helper function that makes setState
calls more declarative and easier to construct, just like how Classnames is a helper function that makes className
values more declarative and easier to construct.
Functional setState is the future of React by Justice Mba
Best kept React secret: you can declare state changes separately from the component classes. by Dan Abramov
FAQs
Compose multiple setState or getDerivedStateFromProps updaters in React
The npm package compose-state receives a total of 4 weekly downloads. As such, compose-state popularity was classified as not popular.
We found that compose-state 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.