A set of hooks to manage/transform/filter states with monads in React
npm install react-state-monad


A set of hooks to manage/transform/filter states with monads in React.
react-state-monad provides a set of monadic state management utilities, specifically designed to work seamlessly with
React's state hooks. It allows you to manage, transform, and filter states in a functional and declarative way using
monads like Maybe and Option.
This library leverages the power of monads to encapsulate state changes, enabling a cleaner, more predictable way to
handle various state conditions in React.
- Manage state using monads like Maybe and Option.
- Simplify handling of undefined or null values in state.
- Leverage functional programming patterns in React state management.
- Support for TypeScript with full type definitions.
You can install react-state-monad using npm or yarn:
``bash`
npm install react-state-monad
or
`bash`
yarn add react-state-monad
Here's an example of how you can use the hooks in your React components:
This hook initializes a StateObject with the provided initial value. It uses React's useState hook to manage the
internal state and returns a StateObject that represents the current state.
Parameters: initialState: The initial value of the state. This value can be of any type, determined by the genericT
type .
Returns a StateObject of type T representing the initialized state. This StateObject is an instanceValidState
of , whichsetValue
encapsulates the state value and provides a function to update it.
This hook initializes a StateObject with an empty state. It is useful as a fallback when no valid state is available.
Parameters: None.
Returns a StateObject of type T representing an empty state. This StateObject is an instance of EmptyState,
which encapsulates the absence of a state value.
This hook allows you to derive and update a specific element in an array within a StateObject. It is useful for
managing state at a granular level within an array.
Parameters:
- state: The StateObject containing an array.index
- : The index of the element to be derived.
Returns a StateObject of type T representing the element at the given index. If the index is out of bounds or theEmptyState
state is empty, it returns an instance of . Otherwise, it returns an instance of ValidState, whichsetValue
encapsulates the element value and provides a function to update it.
This hook derives a field from the state object and creates a new StateObject for the field's value. It is useful for
managing state at a granular level within an object.
Parameters:
- state: The StateObject containing the original state.field
- : The field name to be derived from the state.
Returns a StateObject of type TField representing the derived field. This StateObject is an instanceValidState
of , which encapsulates the field value and provides a setValue function to update it.
For example:
`jsx
import React from 'react';
import {useStateObject, useFieldState} from 'react-state-monad';
const MyComponent = () => {
const userState = useStateObject({
name: 'John Doe',
age: 30,
});
const nameState = useFieldState(userState, 'name');
const ageState = useFieldState(userState, 'age');
return (
export default MyComponent;
`
This hook maps each element in an array within a StateObject to a new StateObject, allowing for independent updates
of each element while keeping the overall array state synchronized.
Parameters:
- state: The StateObject containing an array.
Returns an array of new StateObjects, each representing an element in the original array. This allows individual
updates while keeping the array state synchronized. If the state has no value, it returns an empty array.
This hook ensures a StateObject contains a defined, non-null value. If the StateObject's value is undefined or null, it returns an EmptyState. Otherwise, it returns a ValidState with the value and a setter to update the value.
Parameters:
- state: The StateObject which may contain a value, undefined, or null.
Returns a StateObject of type TOrigin representing the value if it is defined and non-null, otherwise an EmptyState.
This hook remaps the keys of a state object to a record of StateObjects, allowing for independent updates of each key while keeping the overall object state synchronized.
Parameters:
- state: The StateObject containing the original state.
Returns a record where each key is mapped to a new StateObject representing the value of that key, allowing individual updates while keeping the object state synchronized. If the state has no value or is an array, it returns an empty object.
Here's a more complete example demonstrating the usage of useStateObject, useFieldState, useElementState,useRemapArray
and hooks in a React component:
`tsx
const AgeField = (props: { ageState: StateObject;
type="number"
value={props.ageState.value}
onChange={x => props.ageState.value = parseInt(x.target.value, 10)}
/>
const NameField = (props: { nameState: StateObject
return ;
type="text"
value={props.nameState.value}
onChange={x => props.nameState.value = x.target.value}
/>
}
const HobbyField = (props: { hobbyState: StateObject
return ;
type="text"
value={props.hobbyState.value}
onChange={x => props.hobbyState.value = x.target.value}
/>
}
const HobbiesField = (props: { hobbiesState: StateObject
const hobbyStates: StateObject
const addHobby = () => {
// Always use the setter to update arrays, do not modify them directly to ensure React state consistency.
// Immutability is key 💗
props.hobbiesState.value = [...props.hobbiesState.value, ''];
}
return
};
export const UserProfile = () => {
type DudeData = {
name: string;
age: number;
hobbies: string[];
}
// Initialize state with an object containing user details and an array of hobbies
const userState: StateObject
name: 'John Doe',
age: 30,
hobbies: ['Reading', 'Traveling', 'Cooking'],
});
// Derive state for individual fields
const nameState: StateObject
const ageState: StateObject
// Derive state for hobbies array
const hobbiesState: StateObject
return (
Contributing
Contributions are welcome! If you'd like to contribute to this library, please fork the repository and submit a pull
request.
How to Contribute
Fork the repository.
* Create a new branch for your feature
git checkout -b feature-name
* Commit your changes git commit -am 'Add new feature'
* Push to the branch git push origin feature-name
* Open a pull request. I'll be happy to review it!License
This project is licensed under the GPL-3.0 License.
Author
Marcos Alvarez`