use-schema-org-action v0.0.0-main.b1d4618
use-schema-org-action
React hooks handling logics of Schema.org Actions.
Background
Demo
Click here for our live demo.
How to use
const [action, setAction, performAction, isValid] = useSchemaOrgAction(
{
actionOption: 'upvote',
'actionOption-input': {
valueName: 'action'
}
},
useCallback((request, values) => {
request === { actionOption: 'upvote' };
values.get('action') === 'upvote';
}, [])
);
action === { actionOption: 'upvote', actionStatus: 'PotentialActionStatus' };
setAction({ actionOption: 'downvote' });
isValid && performAction();API
useSchemaOrgAction<Action, Request, Response>(initialAction: Action, handler: (request: Request, values: Map<string, unknown>): Promise<Response>): [
Action,
Dispatch<SetStateAction<Action>>
(): Promise<void>,
boolean
]Designs
Communicate with actionStatus and leave other properties open
Except actionStatus, other properties are not controlled.
When performAction is called, it will set actionStatus accordingly:
- Initially,
actionStatuswill be"PotentialActionStatus"; - When
performAction()is being called,actionStatuswill become"ActiveActionStatus"; - When
performAction()is being resolved,actionStatuswill become"CompletedActionStatus"; - When
performAction()is being rejected,actionStatuswill become"FailedActionStatus".
In special circumstances:
- If
actionStatusis set ininitialAction, its value will be used, replacing"PotentialActionStatus"; - If
actionStatus-outputis set ininitialAction, afterperformAction()is resolved,actionStatusfrom the response will be used, replacing"CompletedActionStatus".
All input/output properties must have their constraints defined
Properties of action that would participate in request must have their input constraints defined. Similarly, properties of response that would merge into action must have their output constraints defined.
All constraints must be defined in initialAction and cannot be modified later.
Behaviors
Some properties are not passed to the handler
Make sure all properties that should be part of the request have input constraints (*-input) defined.
Only properties with input constraints will become part of the request.
Some results are not reflected in the updated action
Make sure all results have output constraints (*-output) defined.
After an action is performed, only results marked with output constraints are propagated to the updated action.
After the action is performed, how can I propagate the action status to the updated action?
Marks the action with actionStatus-output. In the handler, returns actionStatus with a supported value. It will be propagated to the updated action.
If the handler did not respond with actionStatus or not output constraints is defined, it will set actionStatus to "CompletedActionStatus" for resolutions, or "FailedActionStatus" for rejections.
Why the performAction function is invalidated on every re-render?
The handler function (second argument) should be memoized via useCallback.
Every time the handler function is changed, the performAction will be invalidated.
Contributions
Like us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago