Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
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.
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
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.