react-router-form

<Form> is to <form> as <Link> is to <a>

React Router <Form>

Travis npm package Coveralls

A <Form> component for use with React Router which does for <form> what react-router’s <Link> does for <a>.

Usage

A <Form> component renders a <form> element with the contents you provide, hooks into the form’s onSubmit event to extract user input and transitions to the configured route with user input data in the next location’s state.

import React from 'react'
import Form from 'react-router-form'

let NewPost = React.createClass({
  render() {
    <Form to={`/topics/${this.props.params.topicId}/add-post`} method="POST">
      <textarea name="comment"/>
      <button type="submit">Add Post</button>
    </Form>
  }
})

For example, assuming you have the following routes, the component above would trigger the onEnter handler for the add-post route when the form is submitted:

<Route path="/topics/:topicId/new-post" component={NewPost}/>
<Route path="/topics/:topicId/add-post" onEnter={handleAddPost}/>

Form data is set as a body property and the form’s method is set as a method property in the next location’s state:

function handleAddPost(nextState, replaceState) {
  console.log(nextState.location.state.method) // 'POST'
  console.log(nextState.location.state.body) // {comment: '...'}
}

Goals

One of the key goals of this component is to make it easier to implement basic isomorphic forms in your React app.

If your onEnter handlers send back everything needed to re-render a form which has errors (i.e. validation errors and user input), then for a little extra effort your React components can handle form submissions on both client and server:

function handleAddPost(nextState, replace, callback) {
  let comment = nextState.location.state.body
  let {topicId} = nextState.params
  ForumService.addComment({comment, topicId})
    .then(({page}) => {
      replace(`/topics/${topicId}?page=${page}`)
    })
    .catch(errors => {
      replace({
        pathname: `/topics/${topicId}/new-post`,
        state: {comment, errors}.
      })
    })
    .finally(callback)
}

Install

”“sh

For React Router 3.x

npm install react-router-form

For React Router 1.x

npm install [email protected]


```js
import Form from 'react-router-form'
// or
const Form = require('react-router-form')

Browser bundles are available, which export a global ReactRouterForm variable and expect to find a global React variable to work with.

MIT Licensed

Related Repositories

jackblog-react

jackblog-react

Jackblog react 版, 个人博客系统, 使用服务端渲染(Universal / Isomorphic), react, redux, react-router, react-bootstrap, immutablejs, redux-form等 ...

react-auth-app-example

react-auth-app-example

An app example with authentication using Create React App, React, React Router, Apollo, GraphQL, Redux and Redux Form. ...

react-router-form

react-router-form

<Form> is to <form> as <Link> is to <a> ...

react-redux-boilerplate

react-redux-boilerplate

React.js/Redux/Redux-form/React-router/Immutable.js/Webpack/ES6/Eslint/Less ...

euro16

euro16

Analytics for Euro 2016 - React / Redux / React-router / Redux-form / Redux-thunk ...