Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Feb 4, 2024
1 parent e6c2b5f commit 6ad2601
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions README.md
Expand Up @@ -11,24 +11,33 @@ Click here for [our live demo](https://compulim.github.io/use-schema-org-action/
## How to use

```ts
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();
function submitVote(request, values) {
// Request is created from action property which has an input constraint.
request === { actionOption: 'upvote' };

// Values is created from action property which has a named input constraint.
values.get('action') === 'upvote';
}

function VoteButton() {
const [action, setAction, performAction, isValid] = useSchemaOrgAction(
{
actionOption: 'upvote',
'actionOption-input': {
valueName: 'action'
}
},
submitVote
);

// Action exclude all input/output constraints, and include "actionStatus" property.
action === { actionOption: 'upvote', actionStatus: 'PotentialActionStatus' };

// To modify the action.
setAction({ actionOption: 'downvote' });

return <button disabled={!isValid} onClick={performAction}>Vote</button>
}
```

## API
Expand Down Expand Up @@ -59,9 +68,9 @@ In special circumstances:
- If `actionStatus` is set in `initialAction`, its value will be used, replacing `"PotentialActionStatus"`
- If `actionStatus-output` is set in `initialAction`, after `performAction()` is resolved, `actionStatus` from the response will be used, replacing `"CompletedActionStatus"`
### All input/output properties must have their constraints defined
### Request/response are based on input/output constraints
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.
Properties of action that would participate in a 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.
Expand All @@ -85,11 +94,11 @@ Marks the action with `actionStatus-output`. In the handler, returns `actionStat
If the handler did not respond with `actionStatus` or output constraints is not defined, it will set `actionStatus` to `"CompletedActionStatus"` for resolutions, or `"FailedActionStatus"` for rejections.
### Why the `performAction` function is invalidated on every re-render?
### Why the `performAction` function is being invalidated on every re-render?
The handler function (second argument) should be memoized via `useCallback`.
The `handler` function (passed as second argument) should be memoized via `useCallback`.
When the handler function change, the `performAction` will be invalidated.
When a different `handler` function is passed, the `performAction` will be invalidated.
## Contributions
Expand Down

0 comments on commit 6ad2601

Please sign in to comment.