0ui 中文文档教程
一组用于使用 React 构建 UI 的实用程序
v1.2.0
npm i 0ui
查看一切如何运行的示例在 example/src/index.js
UMD build
<script src="https://unpkg.com/0ui/dist/0ui.umd.js"></script>
core lib
lib/core.js
Random utilities
Stupid pattern matching
中一起使用 更像是 switch 的简写/case
import { match } from "0ui/lib/core";
match("loading")({
loading: () => console.log("Loading"),
error: () => console.log("Error")
});
// "Error"
imtbl
纯 JavaScript 对象的不可变样式数据操作,请参阅 imtbl 库。
State
lib/state.js
State object
独立状态对象
import State from "0ui/lib/state";
const state = State.create(0);
state.addWatch("key", (key, state, prevVal, nextVal) => {
console.log(prevVal, nextVal);
});
state.swap(val => val + 1); // 0 1
Derived state
lib/derived-state.js
导出状态 -> state' 具有一个函数并从 state 传播更新 -> state'
import State from "0ui/lib/state";
import DerivedState from "0ui/lib/derived-state";
const bucket = State.create({
items: [
{
price: 104.5
},
{
price: 78.2
}
]
});
const bucketPrice = DerivedState.create(bucket, ({ items }) =>
items.reduce((sum, { price }) => sum + price, 0)
);
bucketPrice.state; // 182.7
bucket.swap(removeFirstItem);
bucketPrice.state; // 178.2
Multimethod
lib/multimethod.js
多重分派或多方法是一些编程语言的一个特性,其中一个函数或方法可以根据运行时(动态)类型或,在更一般的情况下,它的参数不止一个的一些其他属性。 维基百科
import MultiMethod from "0ui/lib/multimethod";
const toJSON = MultiMethod.create(v => {
if (Array.isArray(v)) {
return "array";
} else {
return typeof v;
}
});
toJSON.addMethod("string", v => JSON.stringify(v));
toJSON.addMethod("number", v => v.toString());
toJSON.addMethod("array", arr => {
const jsonArr = arr.map(v => toJSON.dispatch(v)).join(", ");
return `[${jsonArr}]`;
});
toJSON.addMethod("object", o => {
const jsonObj = Object.entries(o)
.map(([k, v]) => toJSON.dispatch(k) + ":" + toJSON.dispatch(v))
.join(", ");
return `{${jsonObj}}`;
});
toJSON.dispatch({
items: [1, "two"]
});
// {"items":[1, "two"]}
UI
lib/ui.js
React 组件 API 包装器。 订阅多个状态对象。
import { createComponent } from "0ui/lib/ui";
import State from "0ui/lib/state";
const Counter = createComponent({
states: {
count: State.create(0) // local state
},
render(states, props) {
const { count } = states;
return (
<div>
<button onClick={() => count.swap(v => v - 1)}>-</button>
<span>{count.state}</span>
<button onClick={() => count.swap(v => v + 1)}>+</button>
</div>
);
}
});
A set of utilities for building UIs with React
v1.2.0
npm i 0ui
See an example of how everything is used together in example/src/index.js
UMD build
<script src="https://unpkg.com/0ui/dist/0ui.umd.js"></script>
core lib
lib/core.js
Random utilities
Stupid pattern matching
More like a shorthand for switch/case
import { match } from "0ui/lib/core";
match("loading")({
loading: () => console.log("Loading"),
error: () => console.log("Error")
});
// "Error"
imtbl
Immutable style data manipulation for plain JavaScript objects, see imtbl lib.
State
lib/state.js
State object
Independent state object
import State from "0ui/lib/state";
const state = State.create(0);
state.addWatch("key", (key, state, prevVal, nextVal) => {
console.log(prevVal, nextVal);
});
state.swap(val => val + 1); // 0 1
Derived state
lib/derived-state.js
Derives state -> state' with a function and propagates updates from state -> state'
import State from "0ui/lib/state";
import DerivedState from "0ui/lib/derived-state";
const bucket = State.create({
items: [
{
price: 104.5
},
{
price: 78.2
}
]
});
const bucketPrice = DerivedState.create(bucket, ({ items }) =>
items.reduce((sum, { price }) => sum + price, 0)
);
bucketPrice.state; // 182.7
bucket.swap(removeFirstItem);
bucketPrice.state; // 178.2
Multimethod
lib/multimethod.js
Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case some other attribute, of more than one of its arguments. Wikipedia
import MultiMethod from "0ui/lib/multimethod";
const toJSON = MultiMethod.create(v => {
if (Array.isArray(v)) {
return "array";
} else {
return typeof v;
}
});
toJSON.addMethod("string", v => JSON.stringify(v));
toJSON.addMethod("number", v => v.toString());
toJSON.addMethod("array", arr => {
const jsonArr = arr.map(v => toJSON.dispatch(v)).join(", ");
return `[${jsonArr}]`;
});
toJSON.addMethod("object", o => {
const jsonObj = Object.entries(o)
.map(([k, v]) => toJSON.dispatch(k) + ":" + toJSON.dispatch(v))
.join(", ");
return `{${jsonObj}}`;
});
toJSON.dispatch({
items: [1, "two"]
});
// {"items":[1, "two"]}
UI
lib/ui.js
React Component API wrapper. Subscribes to multiple State objects.
import { createComponent } from "0ui/lib/ui";
import State from "0ui/lib/state";
const Counter = createComponent({
states: {
count: State.create(0) // local state
},
render(states, props) {
const { count } = states;
return (
<div>
<button onClick={() => count.swap(v => v - 1)}>-</button>
<span>{count.state}</span>
<button onClick={() => count.swap(v => v + 1)}>+</button>
</div>
);
}
});
你可能也喜欢
- 7hoo 中文文档教程
- @12stonechurch/react-native-video-controls 中文文档教程
- @2sic.com/dnn-sxc-angular 中文文档教程
- @33cn/wallet-api 中文文档教程
- @36node-fcp/ecs-api-sdk-js 中文文档教程
- @721labs/partial-common-ownership 中文文档教程
- @78d6/eslint-config-react-app 中文文档教程
- @8base/utils 中文文档教程
- @abandonware/eddystone-beacon-scanner 中文文档教程
- @abb-cli-dev/core 中文文档教程