Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
react-ctx-store
Advanced tools
A React context wrapper, which makes context more performant, scalable, and easy to use.
A React context wrapper, which makes context more performant, scalable and easy to use.
React context is good, but it is not good for medium to big projects, it has following problems:
npm install --save react-ctx-store
import React, { Component } from 'react'
import { createContext } from 'react-ctx-store'
const store = {
count: 0,
}
const updaters = {
updateCount: (store) => {
return {
count: store.count + 1;
}
}
}
const {Consumer, Provider} = createContext(store, updaters)
function CounterComponent ({count, updateCount}) {
return (
<div>
<span>{count}</span>
<button onClick={updateCount}>update count</button>
</div>
)
}
export function ExampleApp (){
return (
<Provider>
<Consumer mapContextToProps={['count', 'updateCount']}>
<CounterComponent/>
</Consumer>
</Provider>
)
}
import { creatContext } from 'react-ctx-store'
const store = {
count: 0,
}
// an object of where every key should be an string and value should be a function which will update the store
const updaters = {
updateCount: (store, ...rest) => {
return {
count: store.count + 1;
}
}
}
const { Consumer, Provider, connect, useStore, _context } = createContext(
store,
updaters
)
react-ctx-store
exposes one single method createContext
which takes 2 arguments
createContext
returns following Components and methods:
A React component allows consuming components to subscribe to context changes, similar to React Context Provider.
Usage:
function Example() {
return (
/* children of the Provider will have access to store and updaters using Provider Component */
<Provider>
<App />
</Provider>
)
}
A React Component that subscribe to the values of the store/context.
Consumer takes a single prop mapContextToProps
which take an array of properties or a callback function as value
Usage:
/* all direct children of Consumer component will receive count as a prop */
<Consumer mapContextToProps={['count']}>...</Consumer>
or
/* all direct children of Consumer component will receive totalCount as a prop */
<Consumer mapContextToProps={({count} => {totalCount: count * 10})}>
...
</Consumer>
A method which lets you subscribe to the values of the store/context.
Usage:
/* Component will receive count as a prop*/
const App = connect(['count'])(Component)
or
/* Component will receive count as a prop*/
const App = connect((store) => {count: store.count, update: store.updateCount})(Component)
A hook, which returns store and updaters.
Note: the component using useStore hook will re-render if any value changes in the store.
function WithHooks() {
const { count, updateCount } = useStore()
return (
<div>
<span>{count}</span>
<button onClick={updateCount}>Update Count</button>
</div>
)
}
A React Context object which is being used to store the the values, it is being exposted in cause a user wants to have acess to original Context.
import { createContext } from 'react-ctx-store'
const store = {
userData: null
}
const updaters = {
fetchUserData: async (store, userId) => {
const userData = await fetch('.../user/' + userId)
return { userData }
}
}
const { Consumer, Provider } = createContext(store, updaters)
function UserProfile({ userData, fetchUserData }) {
useEffect(() => {
fetchUserData('123')
}, [])
return <div>UserName: {userData.name}</div>
}
export function App() {
return (
<Provider>
<Consumer mapContextToProps={['userData', 'fetchUserData']}>
<UserProfile />
</Consumer>
</Provider>
)
}
MIT © aadilhasan
FAQs
A React context wrapper, which makes context more performant, scalable, and easy to use.
The npm package react-ctx-store receives a total of 0 weekly downloads. As such, react-ctx-store popularity was classified as not popular.
We found that react-ctx-store 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
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.