
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
clean-state
Advanced tools
🐻 一款纯净小巧的状态管理器,使用react-hooks原生实现,自动连接module组织架构。🍋如果你不是要制造一艘航空母舰又厌烦了复杂且难用的大型状态管理库,那么不妨来试试Clean-State。它小巧玲珑、性能极致完全可以满足你的需求。
npm i clean-state --save
// user.ts
export default {
state: {
name: ''
},
reducers: {
setName(payload, currentState) {
const { name } = payload
return {...currentState, name}
}
},
effects: {
async getUser{payload, rootState, dispatch}) {
const { uId } = payload
const user = await fetch.get(`xxx?uid=${uId}`)
dispatch.user.setName({name: user.name})
}
},
};
// index.ts
import createContainer, { bootstrap } from 'clean-state';
import user from 'user'
const modules = {user}
const setUp = bootstrap(modules);
const cState = createContainer(setUp.useHook);
export const { initialState } = setUp;
export const { Provider, useModule } = cState;
// app.ts
import { Provider, initialState } from 'index.ts';
function MyApp({ Component, pageProps }: AppProps): React.ReactElement {
// todo: here can modify initialState
return <Provider initialState={initialState}>
<Component {...pageProps} />
</Provider>;
}
// page.ts
import { useModule } from 'index.ts';
function Page() {
const [state: {user}, dispatch] = useModule()
const change = useCallback(()=> {
const payload = { name: 'test' }
dispatch.user.setName(payload)
}, [])
return <div>
<button onClick={change}>modify</button>
{user.name}
<div>
}
在很多情况下,多个module之间会存在公共的state、reducers或者effects。
// common.ts
export default {
reducers: {
setValue<T>(payload: Record<keyof T, any>, state: T): T {
return { ...state, ...payload };
},
},
};
// index.ts
import commont from 'common'
import user from 'user'
import { mixin } from 'src/store';
// user 模块混入了common的setValue方法
const modules = mixin(common, { user })
...
Dispatch优先级为 effects -> reducers,同模块下函数不允许同名。
FAQs
English | 中文
The npm package clean-state receives a total of 0 weekly downloads. As such, clean-state popularity was classified as not popular.
We found that clean-state 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
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.