
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
react-redux-local
Advanced tools
[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
I love redux, but building a small and simple local reducer component on every project is not on top of the list of things I like to do the most, plus what if I want to take advantage of sagas, dev tools and the new context api? It becomes a not so simple component very quickly.
You can think of react-redux-local
as a mini, yet powerful version of react-redux, the api is very simple, abstracting away things like creating a redux store, adding middleware, binding actions and plugging in the redux dev tools.
This module is distributed via npm which is bundled with node and
should be installed as one of your project's dependencies
:
yarn add react-redux-local
import LocalReducer from 'react-redux-local'
// https://github.com/erikras/ducks-modular-redux
import { actions, reducer, saga, middleware, devToolsOptions } from './duck'
const App = () => (
<LocalReducer
actions={actions}
reducer={reducer}
saga={saga}
middleware={middleware}
devToolsOptions={devToolsOptions}
>
{(state, actions, dispatch) => (
<YourComponent state={state} actions={actions} />
)}
</LocalReducer>
)
import { createContext } from 'react-redux-local'
import { actions, reducer, saga, middleware, devToolsOptions } from './redux'
const { Provider, Consumer } = createContext({
actions,
reducer,
saga,
middleware,
devToolsOptions,
})
const Up = () => (
<Consumer mapActions={({ countUp }) => countUp}>
{(_, action) => <button onClick={action}>UP</button>}
</Consumer>
)
const Down = () => (
<Consumer mapActions={({ countDown }) => countDown}>
{(_, action) => <button onClick={action}>DOWN</button>}
</Consumer>
)
// Will only rerender when the "counter" state changes
const Count = () => (
<Consumer mapState={({ counter }) => counter}>
{state => <h3>Count: {state}</h3>}
</Consumer>
)
// Will only rerender when the "total" state changes
const TotalCount = () => (
<Consumer mapState={({ total }) => total}>
{state => <h3>Total count: {state}</h3>}
</Consumer>
)
// Will only rerender when the "downs" state changes
const DownsOnly = () => (
<Consumer mapState={({ downs }) => downs}>
{state => <h3>Downs: {state}</h3>}
</Consumer>
)
const App = () => (
<Provider>
<Up />
<Down />
<Count />
<TotalCount />
<DownOnly />
</Provider>
)
Tip:
createContext
takes the same props asLocalReducer
reducer
func.isRequired
A reducer specifies how the application's state changes in response to actions sent to the store.
e.g.
const initialState = { counter: 0, total: 0, downs: 0 }
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'COUNT_UP':
return {
counter: state.counter + 1,
total: state.total + 1,
downs: state.downs,
}
case 'COUNT_DOWN':
return {
counter: state.counter - 1,
total: state.total + 1,
downs: state.downs + 1,
}
default:
return state
}
}
actions
objectOf(func.isRequired).isRequired
Actions are payloads of information that send data from your application to your store. They are the only source of information for the store.
e.g.
const actions = {
countUp: () => ({ type: 'COUNT_UP' }),
countDown: () => ({ type: 'COUNT_DOWN' }),
}
saga
func
Aims to make application side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) easier to manage, more efficient to execute, simple to test, and better at handling failures.
e.g.
import { put } from 'redux-saga'
function* doubleCount() {
put(actions.countUp())
}
function* saga() {
yield takeEvery('COUNT_UP', doubleCount)
}
middleware
arrayOf(func.isRequired)
It provides a third-party extension point between dispatching an action, and the moment it reaches the reducer.
const middleware = store => next => action => {
console.log(action.type)
return next(action)
}
devToolsOptions
object
Allows for a better development experience with redux.
e.g.
const devToolsOptions = { name: 'My own devtools tab' }
children
func.isRequired
The term “render prop” refers to a simple technique for sharing code between React components using a prop whose value is a function.
Video: Michael Jackson - Never Write Another HoC
<Consumer />
props (from createContext
)mapState
func | state => undefined
Behaves like mapStateToProps
from react-redux
with the exception that it won't be available in the props (duh) and you are not required to return an object (thank you render props)
mapActions
func | (actions, dispatch) => undefined
Allows you to pick what actions you want available in the second argument of your render function. dispatch
is very much optional since all the actions are binded automatically.
<LocalReducer />
render function;(state, actions, dispatch) => <YourComponent />
state
Your application state.
actions
Binded actions. (You don't need to dispatch)
dispatch
Optional function that allows you to dispatch other actions.
dispatch({ type: 'VERY_CUSTOM_ACTION' })
MIT
FAQs
[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
The npm package react-redux-local receives a total of 0 weekly downloads. As such, react-redux-local popularity was classified as not popular.
We found that react-redux-local 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
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.