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

reduxify-form

v0.0.3

Published

Improve redux-form api to give your connected form all of the state it needs!

Downloads

12

Readme

reduxify-form

Improve redux-form api to give your connected form all of the state it needs! :penguin:

Codeship Status for cdaringe/reduxify-form Coverage Status

why

the redux-form API has some quirks. your reduxForm(...)'ed component isn't passed all of the form state you need.

For example:

  • by default, you can't see all of your validation errors, or form values in the root component (see #2299)
  • when you want to get extra props into your reduxForm'd component, you end up doing some double wrapping/connecting of your exports. it's a little wonky!

this package makes setting up your form easier to get all the rich form state you need!

usage

npm install --save reduxify-form

consider the example of how redux-form recommends getting extra state into your form.

// redux-form provided example, simplified/condensed

// boring old imports
import React from 'react'
import { connect } from 'react-redux'
import { Field, reduxForm, formValueSelector } from 'redux-form'

// boring old form component
let MyForm = (props) => {
  const { firstName, handleSubmit } = props
  return (
    <form onSubmit={handleSubmit}>
      <Field name="firstName" component="input" type="text" placeholder="First Name"/>
      <input value={`You've typed: ${firstName}`} />
      <Field name="lastName" component="input" type="text" placeholder="Last Name"/>
    </form>
  )
}

// connect redux-form to our component for the first time...
MyForm = reduxForm({
  form: 'myForm'
})(MyForm)

// connect our component again to get some additional state
MyForm = connect(
  state => {
    const firstName = formValueSelector('myForm')(state, 'firstName')
    return { firstName }
  }
)(MyForm)

export default MyForm

ok, that's not too bad! unforunately, not all selectors work. see #2299. further, this could be a little bit friendlier! let's examine how reduxify-form helps.

// reduxify-form example

// one extra import!
import React from 'react'
import { Field } from 'redux-form'
import { connect } from 'react-redux'
import { reduxifyForm } from 'reduxify-form' // <== this guy :)

let MyForm = (props) => {
  // note where we get our additional state from now
  const { formState: { firstName }, handleSubmit } = props
  return (
    <form onSubmit={handleSubmit}>
      <Field name="firstName" component="input" type="text" placeholder="First Name"/>
      <input value={`You've typed: ${firstName.value}`} />
      <Field name="lastName" component="input" type="text" placeholder="Last Name"/>
    </form>
  )
}

// wait, and this is it?
// yup!  just one call to get all of that additional state (like field values, such as firstName)
export default reduxifyForm({
  component: MyForm,
  formName: 'myform',
  connect
})

api

reduxifyForm({ component: Component, formName, [connect], [getState] }) : Component

export default reduxifyForm({
  component: MyForm,
  formName: 'myform'
})

by default, behaves similarly to reduxForm whilst applying additional form state to your props.

addtional props come through as props.formState. what is exactly in formState, you ask? content from redux-form state selectors.

// selectors are from http://redux-form.com/6.3.1/docs/api/ReduxForm.md/
props.formState = {
  values: getFormValues(formName)(state),
  syncErrors: getFormSyncErrors(formName)(state),
  submitErrors: getFormSubmitErrors(formName)(state)
}

want different content in formState? no problem. pass a custom function via getState.

export default reduxifyForm({
  component: MyForm,
  formName: 'myform'
  getState: function (formName, state) {
    // return any selectors from redux form
    return {
      dirty: isDirty(formName)(state),
      pristine: isPristine(formName)(state),
      invalid: isInvalid(formName)(state),
      myFieldValue: formValueSelector(formName)(state, 'myField'),
      fruit: 'bananas'
    }
  }
})

init(connect) : undefined

rather than passing connect on every form call, you can bind reduxify-form to connect just once, so it works with all of your forms.

import { connect } from 'react-redux'
import { reduxifyForm, init } from 'reduxify-form'
init(connect)

// now you don't need to provided `connect` to any reduxifyForm calls
export default reduxifyForm({
  component: MyForm,
  formName: 'myform',
  connect
})