The Action component is the preferred method of triggering actions from within a component. It uses the Render Prop approach for passing an action as a function to the children prop.
import { Action } from 'perch-data';
import { createNotification } from './myapi'; // createNotification returns a promise
const Notifications = () => (
<div>
<Action action={createNotification} variables={this.state}>
{(create, { data, loading, error }) => (
<div>
<button onClick={create} disabled={loading}>
Create Notification
</button>
</div>
)}
</Action>
</div>
);
Notifications.propTypes = {};
export default Notifications;
- It can be passed
variables
for easy, declarative actions - It can be passed
refetchActions
for simple reactive data updates - It passes
context
to your action so it can use thestore
andapi
- Its just a component so debugging and composing are 🍰
action: Function
- Required Function that returns a promisechildren: Function
- Required Function to use with the Result object (below)refetchData: Array<Object>
action: Function
- Action to call after the mainaction
resolvesvariables: Object
- Same as variables for Action or Data components
variables: Object
- Object to be passed toaction
like so:action(variables)
- Note: you can pass an object to theaction
from the Result arguments (below) when calling, overriding this prop
Whenever the action
is called, it will receive two arguments: variables
and context
. Context is where the store
and api
live. Please use these instead of a custom API or store instance. By using an instance already attached to context, we can leverage the Data component to do reactive updates without any extra code.
0: Function
- The first argument is the actionWithVariablesAndRefetch - this is what you want to call in your component1: Object
:data: Object
- If the action is successful, the result is returned hereloading: Boolean
- this istrue
while the action is being performed. once it is returned or an error is thrown, the value will update tofalse
error: Object
- this Axios error object is returned if Axios throws an exception (404, 500, ERRCON, etc) or error thrown from a promise
import { Action, Data } from 'perch-data';
import { createNotification, getNotifications } from './myapi';
const Notifications = () => (
<div>
<Data action={getNotifications}>
{({ data, error, loading }) => {
if (loading) return <div> Loading... </div>;
if (error) return <div> ERROR! </div>;
if (data) return (
<div>
Notifications: {data.total_count}
</div>
<Action
action={createNotification}
refetchData={[{ action: getNotifications }]}
variables={{ title: 'Hello World', color: 'red' }}
>
{(create, { loading }) => (
<div>
<button onClick={create} disabled={loading}>
Create Notification
</button>
</div>
)}
</Action>
);
}}
</Data>
</div>
);
Notifications.propTypes = {};
export default Notifications;
In the above example, calling create()
will create a new notification and reload the list of notifications (causing the list to re-render). No logic is required to update any stores or refetch any requests - it's all automatic!