All Projects â†’ react-native-toolkit â†’ rex-state

react-native-toolkit / rex-state

Licence: MIT license
Convert hooks into shared states between React components

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to rex-state

jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-59.37%)
Mutual labels:  state-management, state, react-hooks
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-59.37%)
Mutual labels:  state-management, state, react-hooks
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+668.75%)
Mutual labels:  state-management, state, react-hooks
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+68.75%)
Mutual labels:  state-management, state, react-hooks
Statefulviewcontroller
Placeholder views based on content, loading, error or empty states
Stars: ✭ 2,139 (+6584.38%)
Mutual labels:  state-management, state
Hydrated bloc
An extension to the bloc state management library which automatically persists and restores bloc states.
Stars: ✭ 181 (+465.63%)
Mutual labels:  state-management, state
snap-state
State management in a snap 👌
Stars: ✭ 23 (-28.12%)
Mutual labels:  state-management, state
Final Form
🏁 Framework agnostic, high performance, subscription-based form state management
Stars: ✭ 2,787 (+8609.38%)
Mutual labels:  state-management, state
Freactal
Clean and robust state management for React and React-like libs.
Stars: ✭ 1,676 (+5137.5%)
Mutual labels:  state-management, state
React Organism
Dead simple React state management to bring pure components alive
Stars: ✭ 219 (+584.38%)
Mutual labels:  state-management, state
Xstate
State machines and statecharts for the modern web.
Stars: ✭ 18,300 (+57087.5%)
Mutual labels:  state-management, state
Mobx Rest
REST conventions for Mobx
Stars: ✭ 164 (+412.5%)
Mutual labels:  state-management, state
Redux Rs
đŸ“Ļ A Rust implementation of Redux.
Stars: ✭ 158 (+393.75%)
Mutual labels:  state-management, state
React Easy State
Simple React state management. Made with ❤ī¸ and ES6 Proxies.
Stars: ✭ 2,459 (+7584.38%)
Mutual labels:  state-management, state
Contextism
😍 Use React Context better.
Stars: ✭ 141 (+340.63%)
Mutual labels:  state-management, state
Getx pattern
Design pattern designed to standardize your projects with GetX on Flutter.
Stars: ✭ 225 (+603.13%)
Mutual labels:  state-management, state
vuse-rx
Vue 3 + rxjs = ❤
Stars: ✭ 52 (+62.5%)
Mutual labels:  state-management, state
enform
Handle React forms with joy đŸŋ
Stars: ✭ 38 (+18.75%)
Mutual labels:  state-management, state
react-state
React-State - Superpowers for managing local and reusable state in React
Stars: ✭ 14 (-56.25%)
Mutual labels:  state-management, state
atomic-state
Atomic State is a state management library for React
Stars: ✭ 15 (-53.12%)
Mutual labels:  state-management, state

rex-state-logo

Rex State

Convert hooks into shared states between React components

Build Status Maintainability Test Coverage

Version Downloads Bundlephobia

Star on GitHub Watch on GitHub Twitter Follow

donate sponsor support

Storybook Chromatic


PRs Welcome 👍✨

Requirements

Rex State is built purely on React Hooks hence it requires React > 16.8 to work.

Installation

yarn add rex-state

# or

npm i rex-state

Usage

Consider the following hook which lets you toggle theme between light & dark modes

const useThemeMode = (initialTheme = 'light') => {
  const [theme, setTheme] = useState(initialTheme);

  const toggleTheme = () => setTheme(theme === 'light' ? 'dark' : 'light');

  return [theme, toggleTheme];
};

You can use the createRexStore module from rex state to create a provider & a store hook to access the result of your useThemeMode

import { createRexStore } from 'rex-state';

const { useStore: useTheme, RexProvider: ThemeProvider } = createRexStore(
  useThemeMode
);

The useStore hook & RexProvider are renamed to useTheme & ThemeProvider for use in the application.

Now you can wrap your entire Application inside the ThemeProvider to ensure the context is setup properly for the useTheme hook.

const App = () => {
  return (
    <ThemeProvider value="dark">
      {/* Rest of your application */}
      <ToggleButton />
      <ThemeText />
    </ThemeProvider>
  );
};

Note: The value of the argument of useThemeMode function - initialTheme is supplied to the ThemeProvider using the value prop. The value prop only supports a single argument. Hence if your hook requires multiple arguments, you can pass them as a single object

Once you add the ThemeProvider to the top of your application's tree, the child components can now use the useTheme hook to access the result of your useThemeMode hook. This time, when you call toggleTheme in any of the child components, it will cause your entire application tree to re-render & all the components that use the useTheme hook will have the updated value!

The following is a toggle button that toggles the theme when users click on it.

const ToggleButton = () => {
  const [theme, toggleTheme] = useTheme();

  return (
    <View>
      <Text>Is Dark Mode?</Text>
      <Switch value={theme === 'dark'} onValueChange={toggleTheme} />
    </View>
  );
};

The next component is a text block that simply displays the theme's mode

const ThemeText = () => {
  const [theme] = useTheme();

  return (
    <>
      <Text>Theme Mode: </Text>
      <Text>{theme}</Text>
    </>
  );
};

Invoking the toggleTheme function from the <ToggleButton/> component updates the <ThemeText/> component. This means your hook is now a shared state between the two components!

Also, check out the counter example from codesandbox

Rex State is good for some use cases and not suitable for some use cases since it uses the React Context API which is considered inefficient as a change causes the entire React child tree to re-render. Read the performance section to see how to use Rex State effectively.

Why Rex State?

Rex State is a handy utility to make your hooks more powerful. It is simple, un-opinionated & is very tiny!

Licenses

MIT Š DaniAkash

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].