1.0.1 • Published 8 years ago
react-rest-client v1.0.1
react-rest-client
Consuming REST APIs the React way.
Installation
React Rest Client requires React 0.15 or later.
Using npm
npm install --save react-rest-clientUsing yarn
yarn add react-rest-clientUsage
The Basics
React Rest Client borrows many ideas from React Router in terms of syntax. Here is an example of consuming a simple Todo backend API.
Single Endpoint
import React, { Component } from 'react'
import { Client, Endpoint, middleware } from 'react-rest-client'
export const onEnter = fn => event => { if(event.key === 'Enter') fn(event) }
class App extends Component {
  render = () => {
    return (
      <Client base='http://example.com'>
        <Endpoint
          path='todos'
          middleware={[middleware.handleJson()]}
          render={({ response, handlers }) => response ? (
          <div>
            <input type='text' onKeyPress={onEnter(event => {
              handlers.add({ text: event.target.value })
            })} />
            <ul>
              {response.data.map((todo, i) => (
                <li key={i}>
                  <input type='text' placeholder={todo.text} onKeyPress={onEnter(event => {
                    const handler = handlers.bind(todo.uuid)
                    handler.edit({ text: event.target.value })
                    event.target.value = ''
                  })} />
                  <input type='button' onClick={event => handlers.destroy(todo.uuid)}  value='Delete' />
                </li>
              ))}
            </ul>
          </div>
        ) : null} />
      </Client>
    )
  }
}
export default AppMultiple Endpoints
import React, { Component } from 'react'
import { Client, Endpoints, middleware } from 'react-rest-client'
export const onEnter = fn => event => { if(event.key === 'Enter') fn(event) }
class App extends Component {
  render = () => {
    return (
      <Client base='https://example.com'>
        <Endpoint specs={{
          todos: {
            path: 'todos',
            middleware: [middleware.handleJson()],
          }
        }} render={({ todos: { response, handlers } }) => response ? (
          <div>
            <input type='text' onKeyPress={onEnter(event => {
              handlers.add({ text: event.target.value })
            })} />
            <ul>
              {response.data.map((todo, i) => (
                <li key={i}>
                  <input type='text' placeholder={todo.text} onKeyPress={onEnter(event => {
                    const handler = handlers.bind(todo.uuid)
                    handler.edit({ text: event.target.value })
                    event.target.value = ''
                  })} />
                  <input type='button' onClick={event => handlers.destroy(todo.uuid)}  value='Delete' />
                </li>
              ))}
            </ul>
          </div>
        ) : null} />
      </Client>
    )
  }
}
export default App1.0.1
8 years ago
1.0.0-rc.20
8 years ago
1.0.0-rc.19
8 years ago
1.0.0-rc.18
8 years ago
1.0.0-rc.17
8 years ago
1.0.0-rc.16
8 years ago
1.0.0-rc.15
8 years ago
1.0.0-rc.14
8 years ago
1.0.0-rc.13
8 years ago
1.0.0-rc.12
8 years ago
1.0.0-rc.11
8 years ago
1.0.0-rc.10
8 years ago
1.0.0-rc.9
8 years ago
1.0.0-rc.8
8 years ago
1.0.0-rc.7
8 years ago
1.0.0-rc6
8 years ago
1.0.0-rc5
8 years ago
1.0.0-rc4
8 years ago
1.0.0-rc3
8 years ago
1.0.0-rc2
8 years ago
1.0.0-rc1
8 years ago
1.0.0
8 years ago