@5alid/match 中文文档教程

发布于 4年前 浏览 25 项目主页 更新于 3年前

Installation

npm npm i @5alid/match yarn yarn add @5alid/match

Usage

import match from '@5alid/match';

let num = 1;

let matched = match(num)(
  ['_', 'error'], // default case
  [0, () => 'zero'],
  [1, () => 'one'],
  [2, () => 'two'],
  [3, () => 'three']
); // -> 'one'

num = 4;
// this will return 'error'
match(num)(
  ['_', 'error'], // default case
  [0, () => 'zero'],
  [1, () => 'one'],
  [2, () => 'two'],
  [3, () => 'three']
); // -> 'error'

Use Cases

reducers

function stateReducer(state, action) {
  return match(action.type)(
    ['_', () => state],
    ['setSomeProp', () => ({ ...state, someProp: action.payload })],
    ['setOtherProp', () => ({ ...state, otherProp: action.payload })]
  );
}

jsx

获取异步数据的示例按钮

function Status() {
  const [status, setStatus] = useState('IDLE');
  const [data, setData] = useState([]);

  useEffect(() => {
    if (status == 'FETCHING') {
      fetch(api_url)
        .then((res) => res.json())
        .then((json) => {
          setData(json);
          setStatus('SUCCESS');
        })
        .catch(() => setStatus('ERROR'));
    }
  }, [state]);

  return (
    <button onClick={() => setStatus('FETCH')}>
      {/* standard way to handle conditions expression */}
      {match(status)(
        ['_', () => 'Something went wrong'],
        ['FETCHING', () => '...'],
        ['IDLE', () => 'idle'],
        ['SUCCESS', () => 'Done'],
        ['ERROR', () => 'Something went wrong']
      )}
    </button>
  );
}

Installation

npm npm i @5alid/match yarn yarn add @5alid/match

Usage

import match from '@5alid/match';

let num = 1;

let matched = match(num)(
  ['_', 'error'], // default case
  [0, () => 'zero'],
  [1, () => 'one'],
  [2, () => 'two'],
  [3, () => 'three']
); // -> 'one'

num = 4;
// this will return 'error'
match(num)(
  ['_', 'error'], // default case
  [0, () => 'zero'],
  [1, () => 'one'],
  [2, () => 'two'],
  [3, () => 'three']
); // -> 'error'

Use Cases

reducers

function stateReducer(state, action) {
  return match(action.type)(
    ['_', () => state],
    ['setSomeProp', () => ({ ...state, someProp: action.payload })],
    ['setOtherProp', () => ({ ...state, otherProp: action.payload })]
  );
}

jsx

example button that fetches async data

function Status() {
  const [status, setStatus] = useState('IDLE');
  const [data, setData] = useState([]);

  useEffect(() => {
    if (status == 'FETCHING') {
      fetch(api_url)
        .then((res) => res.json())
        .then((json) => {
          setData(json);
          setStatus('SUCCESS');
        })
        .catch(() => setStatus('ERROR'));
    }
  }, [state]);

  return (
    <button onClick={() => setStatus('FETCH')}>
      {/* standard way to handle conditions expression */}
      {match(status)(
        ['_', () => 'Something went wrong'],
        ['FETCHING', () => '...'],
        ['IDLE', () => 'idle'],
        ['SUCCESS', () => 'Done'],
        ['ERROR', () => 'Something went wrong']
      )}
    </button>
  );
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文