humanmade/repress

Name: repress

Owner: Human Made

Description: Connect your Redux store to the WordPress REST API.

Created: 2018-02-06 02:13:02.0

Updated: 2018-05-21 23:36:21.0

Pushed: 2018-04-13 07:43:58.0

Homepage: null

Size: 51

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Repress
Connect your Redux store to the WordPress REST API.
A Human Made project. Maintained by @rmccue.

Repress is a tiny library which takes control of part of your Redux state and handles talking to the WordPress REST API. Repress only owns a piece of your Redux state (called the substate), allowing you to incrementally add it to existing projects and remain in control of your store.

Repress requires Redux and Redux Thunk.

Keep reading for a simple introduction, or dive into the documentation.

Installation

Repress is available on npm, simply add it to your project to get started:

install --save @humanmade/repress

, if you're using Yarn
 add @humanmade/repress

You'll then need to connect it to your store.

The Basics

This library is a reusable tool that you can gradually add to your codebase. You simply create “handlers” for every top-level object (posts or CPTs, comments, terms, and users) you'd like to keep in your Redux store, and the handler takes care of dispatching.

The handler “owns” a piece of your global Redux state called the substate, and handles dispatching and reducing any actions related to it. You can keep the substate wherever you want in your Redux store, allowing you to incrementally adopt the library.

Setting up a handler is a three-step process:

  1. Instantiate a handler with options for the type
  2. Add the reducer to the store
  3. Create actions and helpers using the handler's methods
A Simple Example

Typically, you'll want to have a single types.js file containing the setup for all your types. You can then use in your regular actions.js and reducer.js files used in Redux.

ypes.js
rt { handler } from '@humanmade/repress';

rt const posts = new handler( {
// `type` (required): used to derive the action names, and typically should
// match the object type (post type, taxonomy, etc).
type: 'posts',

// `url` (required): base URL for the type.
url:   window.wpApiSettings.url + 'wp/v2/posts',


egister any static archives up-front.
s.registerArchive( 'home', {} );
s
educer.js
rt { combineReducers } from 'redux';

rt { posts } from './types';

rt default combineReducers( {
// Any regular reducers you have go in here just like normal.

// Then, create a substate for your handlers.
posts: posts.reducer,

In your connected components, you can use the higher-order components (withSingle and withArchive) to pull out data from the substate easily:

ost.js
rt { withSingle } from '@humanmade/repress';
rt React from 'react';

rt { posts } from './types';

t Post = props => <div>
<h1>{ props.post.title.rendered }</h1>
v>;

rt default withSingle(
// Handler object:
posts,

// getSubstate() - returns the substate
state => state.posts,

// mapPropsToId - resolve the props to the post ID
props => props.id
ost );

hen just use <Post id={ 42 } /> !
About

Copyright Human Made. Licensed under the ISC License.

Credits

Created by Human Made to make building React apps with the WordPress REST API even easier. Repress is built using what we've learnt building and scaling React sites. Hire us to work with you!

Written and maintained by Ryan McCue.

Interested in joining in on the fun? Join us, and become human!


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.