1.2.0 • Published 20 days ago

react-async-render-props v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
20 days ago

React Async Render Props

It is a component to build children by resolving a promise with Render Props pattern.

Usage

import AsyncRender, { type ActionType, useActionHandler } from 'react-async-render-props'

function Foo() {
    const actionRef = useActionHandler()
    return (
        <>
            <Button onClick={() => {
                actionRef.current!.runAsync()
            }}>fetch</Button>
            <AsyncRender id='fooId' actionRef={actionRef} request={() => {
                return new Promise<SelectOption[]>(resolve => {
                    setTimeout(() => {
                        resolve([
                            {
                                label: Math.random().toString(36),
                                value: 'xxx'
                            },
                        ])
                    }, 500);
                })
            }}>
                {
                    ({ loading, data, runAsync }) => {
                        return (
                            <Spin spinning={loading}>
                                <Button onClick={() => {
                                    runAsync()
                                }}>fetch</Button>
                                <ProFormSelect options={data}/>
                            </Spin>
                        )
                    }
                }
            </AsyncRender>
        </>
    )
}

export default function App() {
    return (
        <div>
            <div onClick={() => {
                AsyncRender.getHandler('fooId').runAsync()
            }}>clickme</div>
            <Foo />
        </div>
    )
}

API

NameTypeDescription
request(...params: P) => Promise\<T\>Required, A function returning a promise (it will be called when the component is mounted)
children(result: ReturnType\<typeof useRequest\<T, P\>\>) => React.ReactElementRequired, A render props function
actionRefReact.MutableRefObject\<ActionType\<T, P\>\>get useRequest result
options-useRequest options
idstringmark instance
forceUpdatebooleanforce update when fired
AsyncRender.getHandler(id: string) => ActionTypeget action handler
type ActionType<T = any, P extends any[] = any> = ReturnType<typeof useRequest<T, P>> & {
  setParam: (partialParam: any, index?: number) => void
}

License

MIT

1.2.0

20 days ago

1.1.1

6 months ago

1.1.0

8 months ago

1.0.0

8 months ago