Skip to main content

Data creation

Hooks

const [submit, loading] = usePost(url, options);
const [submit, loading] = useSecurePost(url, options);

usePost hooks executes an ajax POST request and returns back the payload sent by the server.
By default, the body of the request is a JSON string.

useSecurePost is similar to usePost but adds a Bearer authorization header that contains the token received and stored by useLoginService in the redux store

note

The data are stored in the state of the component and not in the redux store.

Parameters

Inputs

Mandatory parameters are marked with a *

ParameterTypeDescription
url*stringthe URL to which the Ajax request is sent
optionsAn optional object to specify additional options
onSuccessAppSuccessCallback
  • if onSuccess is a function, this function is called on a successful GET (Promise / async allowed)
  • if onError is a string then the value must be an URL. The hook does a redirect to this URL on a successful GET
Defaults to: Nothing is done on a successful GET
onErrorAppErrorCallback
  • if onError is a function, this function is called if the HTTP response is a 4xx or 5xx (Promise / async allowed)
  • if onError is a string then the value must be an URL. The hook does a redirect to this URL
Defaults to: the hook sends a notification to the error topic
delayLoadingnumberThe number of milliseconds to wait before setting the loading flag to true. This value is useful to not display a loading indicator if the request is executed rapidly.

Defaults to: 0
authobjectContains the credentials to be sent in the request. See Auth.

Defaults to: no credentials sent
headersobjectthe HTTP headers to add in the request

Defaults to: no headers added in the request
paramsobjectParams in the url.
Example: if the user is /user/:userId, options.params could be {userId: '1'}
queryobjectQuery params to add the URL.
Example: If the final url is /users?name=Doe, options.query will be {name: 'Doe'}
note

The options object also accept these additionnal parameters supported by the fetch API

headers,
credentials,
cache,
redirect,
referrer,
referrerPolicy,
integrity,
keepalive,
signal

Outputs

ParameterTypeDescription
submit(data, options) => Promisesubmit is a function that executes the ajax POST request.
data is the body of the POST request
options is the same object as the one used for usePost. Use this object to override an option passed to usePost
loadingbooleanA flag to indicate if the ajax request is pending

Examples

Minimal example

The example below shows how to submit a (fix) JSON object to a server.

examples/cra-data/src/pages/create-user.tsx
loading...

Form submit

The example below shows how to submit data to a server by combining the data library (usePost) and the form library (useForm).

examples/cra-data/src/pages/create-user-form.tsx
loading...

Success redirect

The example below shows how to redirect to the user list page if the user has been successfully created.

examples/cra-data/src/pages/create-user-success-redirect.tsx
loading...