Giter Club home page Giter Club logo

react-router-form's Introduction

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

# For React Router 3.x
npm install react-router-form

# For React Router 1.x
npm install react-router-form@1
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

react-router-form's People

Contributors

insin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-router-form's Issues

Manually submit the form

I'm not sure how to do that but I have a form which I have to submit on every input change.

It's a filter form where the search must happen after each change.

There is a way?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.