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

use-redux-form

v2.1.3

Published

Connect form and Redux store through React hook

Downloads

45

Readme

use-redux-form

npm

With use-redux-form, any kind of React form components can be possible to use simply with Redux store object.

  • Compatible with any component using input tag, select tag.
  • For example <DatePicker {...getFieldProps('createdAt')} />

Install

npm install use-redux-form

Upgrade to v2

// useReduxForm
const { isValidated, isDisabled, values, errors, handleSubmit, getFieldProps } =
  useReduxForm();

// storePath
useReduxForm({ storePath: 'this-is-store-path' }); // v1
useReduxForm('this-is-store-path', {}); // v2
// disable - onDisable
useReduxForm({ disable: () => isLoading }); // v1
useReduxForm('this-is-store-path', { onDisable: () => isLoading }); // v2
// exclude (new)
useReduxForm('this-is-store-path', { exclude: [] }); // v2
// debug (new)
useReduxForm('this-is-store-path', { debug: true }); // v2
// initialValues (new)
// only work when onChange is not given
useReduxForm(
  'this-is-store-path',
  {},
  {
    /* initialValues here */
  },
); // v2

// getFieldProps
const { value, selected, disabled, name, onChange } = getFieldProps('a.2.b', {
  isRequired: true, // no more
  include: [], // new
  transform: false, // new
  key: () => {}, // v1, v2
  exclude: [], // v1, v2
  name: 'my-name-is', // v1, v2
});

Example

Example

API Reference

useReduxForm(options)

import useReduxForm from 'use-redux-form';

const {
  isValidated,
  isDisabled,
  values, // state of redux store
  errors, // return object from validate function
  handleSubmit,
  getFieldProps
} = useReduxForm('user.form', {
  /**
   * Exclude props from `getFieldProps` returns (global)
   * @default []
   * @type {Array}
   */
  exclude: [],

  /**
   * Enable or Disable fields
   * @default F
   * @see {@link https://ramdajs.com/docs/#F}
   * @return {Boolean}
   */
  onDisable: () => isLoading,

  /**
   * Form validate
   * @default always({})
   * @see {@link https://ramdajs.com/docs/#always}
   * @param  {Object} values
   * @return {Object}        empty object = valid
   */
  validate: (values) => {
    const errors = {}

    if (!values.username) {
      errors.username = 'required!'
    }

    return errors;
  },

  /**
   * Transform values before reaching to `value`, `onChange`.
   * @default (o) => o.value
   * @param  {String} name  fieldPath
   * @param  {*}      value
   * @return {*}            a component asks specific data type
   */
  transform: ({ name, value }) => {
    if (name === 'nil-to-zero') {
      return '0'
    }

    if (name === 'date') {
      return new Date(value)
    }

    return value;
  },

  /**
   * General `onChange` prop for all components using `getFieldProps`.
   * @default null
   * @see {@link https://ramdajs.com/docs/#identity}
   * @param  {String} name  fieldPath (getFieldProps)
   * @param  {*}      value value that you typed or stored in Redux store
   */
  onChange: ({ name, value }) => {
    if (name === 'A') {
      actionA(value)
    }

    actionB(value)
  },

  /**
   * Submit function
   * @default @default identity
   * @see {@link https://ramdajs.com/docs/#identity}
   * @param {Object} values
   * @param {Object} errors
   */
  onSubmit: ({ values, errors }) => {
    if (!isEmpty(errors)) {
      return Object.values(errors).forEach((value) => {
        if (value) {
          invalidAlert(value);
        }
      });
    }

    submit(values);
  },

  /**
   * Display current state log on console
   * @default false
   */
  debug: true
}, {
  /*
     - initialValues
     - work only when onChange is not given (experimental feature)
   */
})

return (
  <Field {...getFieldProps('username')} />
  <Button onClick={handleSubmit}>Submit<Button>
);

getFieldProps(fieldPath, options?)

A function return form field props

// reducer/user.js
const initialState = {
  form: {
    username: '',
    someObj: {
      password: '',
    },
    someArray1: ['hello', 'world'],
    someParentState: {
      someArray2: ['world', 'hello'],
      list: [
        {
          id: 1,
          type: 'A',
          name: 'X',
        },
        {
          id: 2,
          type: 'B',
          name: 'Y',
        },
        {
          id: 3,
          type: 'C',
          name: 'Z',
        },
      ],
    },
  },
};

const { value, selected, disabled, name, onChange } = getFieldProps(
  /**
   * fieldPath
   * @type {String}
   * @required
   */
  'someParentState.list.0.type', // => 'A'

  {
    /**
     * Field name
     * @type {String}
     * @default fieldPath
     */
    name: 'userid',

    /**
     * Include props what excluded global (local)
     * @type {Array}
     * @default []
     */
    include: ['name'],

    /**
     * Exclude props from `getFieldProps` returns (local)
     * @type {Array}
     * @default []
     */
    exclude: ['selected'],

    /**
     * Dynamic fieldPath
     * (let's say 'someParentState.list' is `fieldPath`)
     * @param  {Array|Object}  fieldState object from `fieldPath`
     * @param  {Array|Object}  state
     * @return {String|Number}
     */
    key: (fieldState, state) => {
      const idx = fieldState.findIndex((item) => item.id === 2);

      // === someParentState.list.1.type
      return `${idx}.type`;
    },

    /**
     * Force transform stop
     * @type {Boolean}
     */
    transform: false,
  },
);

License

MIT