
Product
Introducing Webhook Events for Pull Request Scans
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
English | 简体中文
React Hook 状态管理器
yarn add rmox
# or
npm install --save rmox
由于 rmox 全局 model 都是挂载在 App 之上所以需要配置 GlobalProvider
只有需要使用全局model的情况下才需要配置
import { StrictMode } from 'react'
import ReactDOM from 'react-dom'
import { GlobalProvider } from 'rmox'
import App from './App'
const rootElement = document.getElementById('root')
ReactDOM.render(
<StrictMode>
<GlobalProvider>
<App />
</GlobalProvider>
</StrictMode>,
rootElement,
)
通过
createModel
创建一个modelHook
,第二个参数为 对应配置global
为是否为全局
const useUserModel = (age: number) => {
const [age, setAge] = useState(age)
const addAge = () => setAge(age => age + 1)
return { addAge, age }
}
export default createModel(useUserModel, {
global: true,
})
在任何组件中直接调用
modelHook
,可以直接获取 model 内容。
import useUserModel from '../models/useUserModel'
const App = () => {
const { age, addAge } = useUserModel()
return (
<>
<button onClick={addAge}>+</button>
{age}
</>
)
}
通过
createModel
创建一个modelHook
import { useState } from 'react'
import { createModel } from 'rmox'
const useCounterModel = init => {
const [count, setCount] = useState(init)
const del = () => setCount(count - 1)
const add = () => setCount(count + 1)
return {
count,
add,
del,
}
}
export default createModel(useCounterModel)
由于 rmox 局部 model 需要一个挂载点,使用需要给局部块添加
Provier
import React from "react";
import Counter from "./components/Counter";
import Count from "./components/Counter/count";
import useCounter from "./models/useCounter";
import useCounterModel from "./models/useCounterModel";
const App = () => {
return (
<div className="App" >
<useCounterModel.Provider value={10}>
<Counter />
<Count />
</useCounterModel.Provider>
</div>
);
}
export default App;
);
直接在需要使用
modelHook
内容的的位置使用useCounterModel
订阅count被修改时会同步更新,只有modelHook内部中当前使用到的数据改变,才会触发当前组件render
import React from 'react'
import useCounterModel from '../../models/useCounterModel'
const Count = () => {
const { count } = useCounterModel()
return (
<>
<div>Count:{count}</div>
</>
)
}
export default Count
import React from 'react'
import useCounterModel from '../../models/useCounterModel'
const Counter = () => {
const { del, add } = useCounterModel()
return (
<>
<button onClick={add}>+</button>
<button onClick={del}>-</button>
</>
)
}
export default Counter
支持用注解绑定(可以绑定多个也可以绑定一个)
@connect([useMoneyModel, useUserModel], ([money, user]) => ({
age: user.age,
money: money.money,
}))
export default class Test extends React.Component {
render() {
const { age, money } = this.props
return (
<>
{money} / {age}
</>
)
}
}
import React from 'react'
import useCounterModel from '../../models/useCounterModel'
const Counter = () => {
const { del, add } = useCounterModel()
return (
<>
<button onClick={add}>+</button>
<button onClick={del}>-</button>
</>
)
}
export default Counter
rmox
支持模块之间的相互依赖
import { useState } from 'react'
import { createModel } from 'rmox'
import useUserModel from './useUserModel'
const useMoneyModel = () => {
const [money, setMoney] = useState(100)
const { addAge } = useUserModel()
const addMoney = () => setMoney(money => money + 1)
return { addMoney, money, addAge }
}
export default createModel(useMoneyModel, {
global: true,
})
model
内容以及修改store
(仅支持全局 model)在实际过程中可能在不是组件的环境中需要获取到
modelHook
内容,rmox
给 model 对象上附带对应的属性犯法
import useUserModel from './useUserModel'
// model内容
const counterState = useUserModel.getData()
// 直接修改内容
useUserModel.getData()?.addAge()
依赖必须为单向流,
禁止循环嵌套
,且全局与局部 model 之间不允许全局依赖局部model
参数 | 描述 | 默认 | 必填 |
---|---|---|---|
useHook | 具体的 modelHook | -- | 是 |
options | 配置 ·· global | 否 |
全局 model 必须配置到入口
FAQs
React Hook 状态管理器
The npm package rmox receives a total of 553 weekly downloads. As such, rmox popularity was classified as not popular.
We found that rmox 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.
Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.