@international-iot-association/rct-state
v3.1.0
Published
Legend-State 风格的 RxJS + Immutable 响应式状态库。
Readme
@international-iot-association/rct-state
Legend-State 风格的 RxJS + Immutable 响应式状态库,供工位插件的 React UI 使用。
属于组 A · 协议锁步组(与 plugin-contracts/plugin-sdk/api-bridge 一起随 agentApi major 锁步发布)。
安装
npm i @international-iot-association/rct-stateUsage
Basic usage for pure JavaScript:
// file: userData.ts
import { observable } from 'rct-state';
// define type for state
interface User {
id: number;
name: string;
}
const user: User = {
id: 1,
name: 'jojo',
}
// create store
export const user$ = observable(user);
// get user id
const userId = user$.id.get();
// set user id
user$.id.set(99);
// watch user id change
user$.observe((state) => {
return state.id
}, (id) => {
console.log('user id changed:', id)
})
Basic usage for React Native or React component:
import React from 'react';
import { View, Text } from 'react-native'
import { user$ } from './userData'
export function UserPanel() {
const { id, name } = user$.use()
const userId = user$.id.use()
const username = user$.useSelector((state) => state.id)
// watch value change
user$.useObserve((state) => state.name, username => {
console.log(`user name changed: ${username}`)
})
return (
<View>
<Text>ID: { id }</Text>
<Text>Name: { name }</Text>
</View>
)
}Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT
Made with create-react-native-library
