39
loading...
This website collects cookies to deliver better user experience
useEffect
/ createEffect
to provide action re-triggers based on specified Store changes.yarn add @gluecodes/storecle-react
npm i @gluecodes/storecle-react
yarn add @gluecodes/storecle-solid
npm i @gluecodes/storecle-solid
import {
builtInActions,
PageProvider,
useAppContext
} from '@gluecodes/storecle-react'
import {
builtInActions,
PageProvider,
useAppContext
} from '@gluecodes/storecle-solid'
See: Code Sandbox example for React.
See: Code Sandbox example for Solid.
.
├── actions
│ ├── dataSuppliers (#2)
│ │ └── index.js
│ ├── reloadTypes.js (#4)
│ └── userActions (#3)
│ └── index.js
├── index.jsx (#1)
├── Layout.jsx (#5)
└── partials (#6)
└── Counter
└── index.jsx
dataSupplierPipeline
- an array providing the order in which Data Suppliers are executed.dataSuppliers
- an object containing Data Suppliers.getLayout
- a function which returns the page Layout.reloadTypes
- an object containing Reload Types.userActions
- an object containing User Actions.onError
- a function triggered when an error is thrown either in Data Suppliers or User Actions../index.jsx
import { PageProvider } from '@gluecodes/storecle-solid'
import * as dataSuppliers from './actions/dataSuppliers/index'
import * as userActions from './actions/userActions/index'
import * as reloadTypes from './actions/reloadTypes'
import Layout from './Layout.jsx'
export default () => (
<PageProvider
dataSupplierPipeline={[
dataSuppliers.getTexts,
dataSuppliers.getCounter
]}
dataSuppliers={dataSuppliers}
getLayout={() => Layout}
reloadTypes={reloadTypes}
userActions={userActions}
onError={(err) => {
console.error(err)
}}
/>
)
buildInActions
- an object containing the following built-in User Actions:
onStoreChanged
- a function which receives a callback to be triggered when Store changes.runUserActions
- a function which allows for executing multiple User Actions at once.runDataSuppliers
- a function which receives a Reload Type name. Note that it's exposed to ease the integration with legacy apps. Don't call it manually as Data Suppliers are implicitly reloaded based on the provided Reload Types.resultOf
and nameOf
.
resultOf
- a function providing a result of a given Data Supplier or User Action.nameOf
- a function providing a name of either Data Supplier, User Action or Reload Type../actions/dataSuppliers/index.js
import { builtInActions } from '@gluecodes/storecle-solid'
import { reFetchCounter } from '../reloadTypes'
export function getCounter (resultOf, nameOf) {
const reloadType = resultOf(builtInActions.runDataSuppliers)
const shouldFetch =
reloadType === 'full' || reloadType === nameOf(reFetchCounter)
if (!shouldFetch) {
return resultOf(getCounter)
}
return global.sessionStorage.getItem('appWideCounter') || 0
}
export function getTexts (resultOf) {
if (resultOf(builtInActions.runDataSuppliers) !== 'full') {
return resultOf(getTexts)
}
return {
Click: 'Click'
}
}
./actions/userActions/index.js
export function incrementCounter (counter) {
const incrementedCounter = Number(counter) + 1
global.sessionStorage.setItem('appWideCounter', incrementedCounter)
}
runDataSuppliers
and reloads all Data Suppliers. nameOf
and returns an array of User Action names.
nameOf
- a function providing a name of User Action../actions/reloadTypes.js
import { incrementCounter } from './userActions/index'
export const reFetchCounter = (nameOf) => [
nameOf(incrementCounter)
]
./Layout.jsx
import Counter from './partials/Counter/index.jsx'
export default () => (
<div className='container'>
<Counter />
</div>
)
useAppContext
- a function which returns an array of 3 items: resultOf
, action
, nameOf
.
resultOf
- a function providing a result of a given Data Supplier or User Action.action
- a function which triggers User Action.nameOf
- a function providing a name of either Data Supplier or User Action../partials/Counter/index.jsx
import { useAppContext } from '@gluecodes/storecle-solid'
import { getCounter, getTexts } from '../../actions/dataSuppliers/index'
import { incrementCounter } from '../../actions/userActions/index'
export default () => {
const [resultOf, action] = useAppContext()
return (
<button
onClick={() => {
action(incrementCounter)(
resultOf(getCounter)
)
}}
>{resultOf(getTexts)?.Click}: {resultOf(getCounter)}</button>
)
}