All Projects → Aslemammad → Contextism

Aslemammad / Contextism

😍 Use React Context better.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Contextism

Yewdux
Redux-like state containers for Yew apps
Stars: ✭ 58 (-58.87%)
Mutual labels:  context, state-management, state
Alveron
Elm & Reason inspired state management for React
Stars: ✭ 57 (-59.57%)
Mutual labels:  context, state-management, state
react-wisteria
Managing the State with the Golden Path
Stars: ✭ 18 (-87.23%)
Mutual labels:  state-management, state, context
Use Global Context
A new way to use “useContext” better
Stars: ✭ 34 (-75.89%)
Mutual labels:  context, state-management, state
React Workshop
⚒ 🚧 This is a workshop for learning how to build React Applications
Stars: ✭ 114 (-19.15%)
Mutual labels:  context, state-management, state
Revived
A redux-inspired predictable state container for python
Stars: ✭ 12 (-91.49%)
Mutual labels:  state-management, state
Concent
State management that tailored for react, it is simple, predictable, progressive and efficient.
Stars: ✭ 882 (+525.53%)
Mutual labels:  state-management, state
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (-5.67%)
Mutual labels:  state-management, state
Reatom
State manager with a focus of all needs
Stars: ✭ 567 (+302.13%)
Mutual labels:  state-management, state
Duix
A State Manager focused on KISS and Pareto's Principle
Stars: ✭ 48 (-65.96%)
Mutual labels:  state-management, state
Freactal
Clean and robust state management for React and React-like libs.
Stars: ✭ 1,676 (+1088.65%)
Mutual labels:  state-management, state
Redux Tree
An alternative way to compose Redux reducers
Stars: ✭ 23 (-83.69%)
Mutual labels:  state-management, state
Pullstate
Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!
Stars: ✭ 683 (+384.4%)
Mutual labels:  state-management, state
Swiftdux
Predictable state management for SwiftUI applications.
Stars: ✭ 130 (-7.8%)
Mutual labels:  state-management, state
Little State Machine
📠 React custom hook for persist state management
Stars: ✭ 654 (+363.83%)
Mutual labels:  state-management, state
Radon
Object oriented state management solution for front-end development.
Stars: ✭ 80 (-43.26%)
Mutual labels:  state-management, state
Reworm
🍫 the simplest way to manage state
Stars: ✭ 1,467 (+940.43%)
Mutual labels:  state-management, state
Machinery
State machine thin layer for structs (+ GUI for Phoenix apps)
Stars: ✭ 367 (+160.28%)
Mutual labels:  state-management, state
Westore
更好的小程序项目架构
Stars: ✭ 3,897 (+2663.83%)
Mutual labels:  state-management, state
React Composition Api
🎨 Simple React state management. Made with @vue/reactivity and ❤️.
Stars: ✭ 67 (-52.48%)
Mutual labels:  state-management, state

Contextism 🤩 is a new way to use React Context better.



picker

Read this article to become familiar with the idea.

License

Installation 🔥

npm i contextism
// or 
yarn add contextism

Usage ✏️

We have two ways to use Contextism, Creating store using it or using its hooks directly:

#1 createStore ✋

// store.js 
import { createStore } from 'contextism';
const context = createStore("default value for state");
export const { Provider, useDispatchContext, useStateContext, useStore } = context;

// App.jsx
import Div from './Div'
import { Provider } from './store';

const App = () => {
	const [ state, dispatch ] = React.useState("Value for state"); // or useReducer
	
	return (
		<Provider state={state} dispatch={dispatch}>
			// Components you want to use the state there.
			<Div />
		</Provider>
		)
}

// Div.jsx
import { useStateContext, useDispatchContext, useStore } from './store';

const Div = () => {
	const state = useStateContext(); // "Value for state"
	const dispatch = useDispatchContext(); // dispatch function (setState) in App
	// or better one
	const [state, dispatch] = useStore();
	
	return ...
}

When we create store using Contextism, it gives us 3 hooks :

  • useStateContext: the state value that we gave it to state prop in Provider component

  • useDispatchContext: the setState function or useReducer dispatch that we passed it to dispatch prop

  • useStore: returns us an array with two values of the above hooks; [ useStateContext, useDispatchContext ]

    NOTE: you should use these hooks( methods of createStore function) in child components of Provider component.

#2 default hooks ✋

Contextism has two hooks beside createStore function:

  • useContext: takes a React context and returns the value
  • useStore: takes two React contexts and returns two values of them, the same thing like in the above way but with two arguments
// Store.jsx
export const CountStateContext = React.createContext();
export const CountDispatchContext = React.createContext();
function countReducer(state, action) {
  ...
}

export function CountProvider({children}) {
  const [state, dispatch] = React.useReducer(countReducer, {count: 0});
  return (
    <CountStateContext.Provider value={state}>
      <CountDispatchContext.Provider value={dispatch}>
        {children}
      </CountDispatchContext.Provider>
    </CountStateContext.Provider>
  )
}
// App.jsx
import { CountProvider } from './Store';
import Div from './Div';
export function App() {
	return (
		<CountProvider>
		<Div />
		</CountProvider>
	)

}
// Div.jsx
import { CountStateContext, CountDispatchContext } from './Store';
import { useContext, useStore} from 'contextism';

export function Div() {
	const state = useContext(CountStateContext);
	const dispatch = useContext(CountDispatchContext);
	// Or much better:
	const [state, dispatch] = useStore(CountStateContext,CountDispatchContext);
	
	return ...

}

Typescript 🔷

Contextism has Typescript support like generics and ... . in createStore you can pass two generics too, first one for the state structure and interface, the second one for the useReducer hook.

type Action = {type: 'increment'} | {type: 'decrement'}
type State = { count: number }
// The second generic is for useReducer Action
const context = createStore<State, Action>();

// For useState just pass the first generic (State interface generic)
const context = createStore<State>();

Donation

You can support me and my projects with Open Collecive.

Contribution

I'm developer, not a perfect person, so I make many mistakes, it means that be free to create issues and then PRs.

Thanks ❤️

Special thanks for contributing and using this project.

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].