
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
React数据缓存和跨级组件通信的轻量级方案
🎯 案例🎯 | ||
---|---|---|
Counter | I18n | Todos |
pnpm add cohook
yarn add cohook
npm i cohook
import createContainer from "cohook"
const container = createContainer(0)
const inc = () => container.commit((draft) => void (draft.current += 1)
const dec = () => container.commit((draft) => void (draft.current -= 1)
function Counter() {
const count = container.useMapDataToState()
return (
<div>
count:{count}
<button onClick={inc}>+</button>
<button onClick={dec}>-</button>
</div>
)
}
createContainer(initialData)
createContainer接收初始数据initialData,返回一个容器对象。
container.getData()
用于获取当前的容器数据,就像是一个内置的action,可在任何地方使用。
container.useMapDataToState(...overloads)
用于将数据状态化,被状态化后的数据会自动跟踪变化并更新,和React Hooks一样,该API必须在Function Component中使用。
const state = container.useMapDataToState()
const derivedState = container.useMapDataToState(mapStateFn)
container.commit(updater)
commit 接受一个函数updater(immer中的produce的第二个参数保持一致)作为参数,用来创建修改数据的Action,可灵活组合容器对象提供的方法来组装Action。
/**
* 1️⃣ Action
*/
const dec = () => {
const count = container.getData()
if(count < 0) return
container.commit((draft) => void (draft.current -= 1)
}
/**
* 2️⃣ Custom Hooks
*/
function useFilter() {
const visibilityFilter = todosContainer.useMapDataToState(data => data.filter)
const setFilter = (filter: VisibilityFilters) => {
todosContainer.commit((draft) => {
draft.current.filter = filter
})
}
return {
filter: visibilityFilter,
setFilter
}
}
FAQs
React数据缓存和跨级组件通信的轻量级方案
The npm package cohook receives a total of 14 weekly downloads. As such, cohook popularity was classified as not popular.
We found that cohook 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.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.