npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-router-form

v3.0.0

Published

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

Downloads

38

Readme

React Router <Form>

[![Travis][build-badge]][build] [![npm package][npm-badge]][npm] [![Coveralls][coveralls-badge]][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
```

```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.

* [react-router-form.js](https://unpkg.com/react-router-form/umd/react-router-form.js) (development version)
* [react-router-form.min.js](https://unpkg.com/react-router-form/umd/react-router-form.min.js) (compressed production version)

## MIT Licensed

[build-badge]: https://img.shields.io/travis/insin/react-router-form/master.png?style=flat-square
[build]: https://travis-ci.org/insin/react-router-form

[npm-badge]: https://img.shields.io/npm/v/react-router-form.png?style=flat-square
[npm]: https://www.npmjs.org/package/react-router-form

[coveralls-badge]: https://img.shields.io/coveralls/insin/react-router-form/master.png?style=flat-square
[coveralls]: https://coveralls.io/github/insin/react-router-form