react-request-block
React component to compose requests within your components, with support for server-side rendered requests.
Install
Via npm:
npm install -S react-request-block
Via Yarn:
yarn add react-request-block
How to use
react-request-block
was built with a focus on simplicity and flexibility, and
with out-of-the-box support for use within server-side rendering frameworks
(more on that below).
RequestBlockProvider
The basic wrapper that provides caching for the nested RequestBlock
instances and
the ability to provide a default origin
to prepend onto the RequestBlock
urls.
Properties
Property | Default | Description |
---|---|---|
cache |
new RequestBlockCache |
RequestBlockCache instance for caching request made per provider. By default, a fresh cache instance is created if one is not supplied. |
options |
null |
Default options to be included with each request made by a RequestBlock . Can be ignored by adding ignoreContextOptions to RequestBlock instances. |
origin |
'' |
Default origin to prepend to RequestBlock requests. Defaults to the current site’s protocol/host. |
renderPromises |
null |
This is used strictly for server-side rendering instances. In most cases you shouldn’t worry about setting this, unless you need to. In which case, it accepts a RenderPromises instance. |
RequestBlock
The RequestBlock
is what you will be interacting with most. It makes it really
easy to compose requests into your pages and components and handles all the caching
for you behind the scenes. A simple alternative to achieving a similar thing
with redux
/react-redux
.
Properties
Property | Default | Description |
---|---|---|
url |
null |
URL to make request to. If origin specified on RequestBlockProvider , url will be appended to it. |
options |
null |
Options to include with fetch request made. All normal fetch options accepted. |
parser |
(data, props) => data |
Method for manipulating the response payload before it is applied to the data value. Handy for stripping out unwanted stuff, or making it more useable for your page or component. |
skip |
false |
Whether or not the request should be skipped during server-side rendering. |
ignoreContextOptions |
false |
Flag to ignore options set on the RequestBlockProvider for requests being made by RequestBlock . |
onRequest |
({ data, error, fetched, loading }) => {} |
Callback made when a request is initiated. |
onLoad |
({ data, error, fetched, loading }) => {} |
Callback made when request has successfull completed. |
onError |
({ data, error, fetched, loading }) => {} |
Callback made when a request encounters an error. |
withRequestBlock
(HOC)
Provides access to the current context of the ReactRequestBlock
. Context passed to the wrapped component includes:
Properties
Property | Type | Description |
---|---|---|
cache |
RequestBlockCache |
The current RequestBlockCache instance being used by the provider. |
options |
object |
null |
origin |
string |
Origin to prepend to urls being requested by RequestBlock instances. |
renderPromises |
RenderPromises |
RenderPromises instance set for use during server-side rendering. |
Examples
Below are a few examples of using the RequestBlockProvider
and two ways you can
use the RequestBlock
.
RequestBlockProvider
;;; // @see Page component defined in `RequestBlock` example below const App = <RequestBlockProvider> <Router> <Switch> <Route path="/:slug*" component=Page /> </Switch> </Router> </RequestBlockProvider>; ;
RequestBlock
(wrapping content)
In this example, the RequestBlock
components allows you to structure requests
right into your pages or components.
;; const Page = <RequestBlock url="[your endpoint here]"> { if loading || !fetched return null; if error console; return null; if !data return <p>Page does not exist</p>; // See the Contentful query response console; // Process and pass in the loaded `data` necessary for your page or child components. return ... ; } </RequestBlock>; ;
RequestBlock
(w/ callbacks)
;; { superprops; thisstate = loading: false data: null error: null ; thisonRequest = thisonRequest; thisonLoad = thisonLoad; thisonError = thisonError; } { this; } { this; } { this; } { return <ReactFragment> <RequestBlock url="[your endpoint here]" onRequest=thisonRequest onLoad=thisonLoad onError=thisonError /> loading && <p>Loading...</p> error && <p>error</p> data && something that uses data </ReactFragment> ; } ;
Using Next.js?
If you like what you see above, you might like next-request-block,
which lets you easily add react-request-block
to your Next.js app. Making it easy
to ensure that all your RequestBlock
instances render awesomely server-side.