• Stars
    star
    142
  • Rank 250,340 (Top 6 %)
  • Language
    TypeScript
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

😍 Use React Context better.

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

picker

Read this article to become familiar with the idea.

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

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.

More Repositories

1

vitext

The Next.js like React framework for better User & Developer experience!
TypeScript
389
star
2

vite-plugin-cloudflare

πŸ”₯ Easier and better experience writing Cloudflare workers in Vite
TypeScript
259
star
3

venode

πŸ’πŸ¦• The missing child of Node.js and Deno.
TypeScript
179
star
4

react-worker-components-plugin

⚑ Something like react server components, but web workers instead of a server
TypeScript
103
star
5

modern-node-polyfills

modern polyfills for node native modules based on jspm-core
TypeScript
39
star
6

deconf

One config to rule them all.
Go
25
star
7

Xmoji

An emoji picker for your OS. Be happy with :)
TypeScript
25
star
8

qwik-pwa-example

Please head over to https://github.com/qwikdev/pwa
TypeScript
17
star
9

Did

A node.js script for changing the wallpaper about your favorite topics.
JavaScript
9
star
10

Cheatsheets

My own fun cheatsheets🐞
8
star
11

Aslemammad

8
star
12

Aloodak-GUI

GUI version of Aloodak
CSS
7
star
13

rock-todo-list

Next generation is telefunc. Not anything else 😎
TypeScript
7
star
14

dotfiles

Shell
5
star
15

hangman

simple game for mastering svg animations. Test state.
TypeScript
4
star
16

simple-shop-api

JavaScript
4
star
17

Bitcoin-Warner

Java
3
star
18

Aloodak-RN

3
star
19

XController

Some additionals for xbox controller in Gnu/linux
TypeScript
3
star
20

react-router-hook

Combine hooks with React-Router. 😍
TypeScript
2
star
21

simple-shop-front

frontend of simple-shop-api
TypeScript
2
star
22

simple-react-bootstrap

Simple uncomplete react bootstrap project (pizza order page)
JavaScript
2
star
23

Jpg-to-png

read the down
Java
2
star
24

leetcode

JavaScript
2
star
25

simpleChart

solve quera.ir challenge
JavaScript
2
star
26

PingS

A game with scratch
2
star
27

fanavard-1399-test

JavaScript
2
star
28

googlehacked.github.io

HTML
1
star
29

Happy-Programmers-Day

Java
1
star
30

static

TypeScript
1
star
31

vite-plugin-vercel

1
star
32

GithubGraph

Don't see this project. Please.
1
star
33

Dynamic-Wallpaper

Shell
1
star
34

bmi

Java
1
star
35

genius-api

nodejs genius api
1
star
36

E-commerce

An open source and free E-commerce
CSS
1
star
37

empty

1
star
38

Date-Project

Java
1
star
39

Vidound

Java
1
star
40

jotai-jsx

TypeScript
1
star