State machine for react hooks to manage async data loading and forms
React-Brix is inspired by react hooks and Redux.
React-brix allows you to implement an immutable app-wide state machine in React without having to write reducers or selectors. Just use a hook to get the data from your state using a path. If the data is not assigned yet, provide a connector to retrieve the data. The data will be stored in state automatically for you.
React ^16.7.0-alpha.2
Immutable.js
To install this :
npm install react-brix --save
or
yarn add react-brix
import { BrixProvider } from 'react-brix'
...
const App = () => {
return (
<React>
<BrixProvider value={initialState}>
<div className='App'>
...
</div>
</BrixProvider>
</React.StrictMode >
)
}
Where the initialBrix value is an immutable Map with any initial data your want for your app.
import { useBrix } from 'react-brix'
import { paths } from '../context'
...
const Name = () => {
const [value, set] = useBrix(paths.name.first.get())
return (
<>
<TextField label='First' value={value} />
...
</>
)
}
export default Name
import { useBrixWorker, BoundedSuspense } from 'react-brix'
import { paths, getAddress } from '../context'
...
const MyWorkingComponent = () => {
const address = useBrixWorker(paths.address.get(), getAddress, Map())
return (
<Address datum={address} />
)
}
...
const AddressWrapper = () => {
return (
<BoundedSuspense
fallback={<div>fetching address...</div>}
boundary={<div>An error ocurred getting the address</div>}
>
<MyWorkingComponent />
</BoundedSuspense>
)
}
export default AddressWrapper