0.1.0 • Published 5 months ago

react-closure-hooks v0.1.0

Weekly downloads
-
License
-
Repository
github
Last release
5 months ago

react-closure-hooks

reexport react hooks, and add useEvent. They are useful for solving the closure problem in react hooks.

NPM version NPM downloads

install

npm install --save react-closure-hooks

or

yarn add react-closure-hooks

usage

// Test
import React from 'react';
import { Search } from 'antd';
import { useState, useEvent } from 'react-closure-hooks';
import API from '@/request';

const Test = () => {
  const [searchKey, setSearchKey] = useState('');
  const [result, setResult] = useState('');

  const doQuery = useEvent(async () => {
    const result = await API.query({
      searchKey
    });
    setResult(result);
  });

  const doSearch = useEvent(async (e) => {
    await setSearchKey(e.target.value);
    doQuery();
  });

  return (
    <div>
      <Search 
        value={searchKey} 
        onSearch={doSearch}
      ><br />
      <div>{result}</div>
    </div>
  );
};

export default Test;

or use useStateWithPromise

// Test
import React, { useState } from 'react';
import { Search } from 'antd';
import { useStateWithPromise, useEvent } from 'react-closure-hooks';
import API from '@/request';

const Test = () => {
  const [searchKey, setSearchKey] = useStateWithPromise('');
  const [result, setResult] = useState('');

  const doQuery = useEvent(async () => {
    const result = await API.query({
      searchKey
    });
    setResult(result);
  });

  const doSearch = useEvent(async (e) => {
    await setSearchKey(e.target.value);
    doQuery();
  });

  return (
    <div>
      <Search 
        value={searchKey} 
        onSearch={doSearch}
      ><br />
      <div>{result}</div>
    </div>
  );
};

export default Test;

License

MIT

0.1.0

5 months ago

0.0.9

6 months ago

0.0.8

6 months ago

0.0.7

12 months ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago