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

formik-redux

v4.2.0

Published

Formik + Redux

Downloads

39

Readme

Formik + Redux

Combines the performance of Formik with the state control of Redux

How it works

The start and stop of the form submit, is controlled by de redux state while all the other features is controlled by Formik

When you submit your form, you should:

import { compose } from "redux";
import { connect } from "react-redux";
import { startSubmit, stopSubmit, withForm } from "formik-redux";

const FORM_NAME = "yourForm";

export const mapDispatchToProps = dispatch => ({
    onSubmit () {
        dispatch( startSubmit( FORM_NAME ) );
    },
    onSubmitSuccess () {
        dispatch( stopSubmit( FORM_NAME ) );
    }
});

const DummyForm = props => <div { ...props }/>;

export default compose(
    connect( null, mapDispatchToProps ),
    withForm({
        form: FORM_NAME, //required
        handleSubmit: ( values, { onSubmit, submitSuccess } ) => {
            onSubmit();
            onSubmitSuccess();
        },
        // ...form props of Formik
    })
)( DummyForm );

Motivation

Redux-form has became too much complex and lacks performance, in the other hand, Formik is much more simple and has better performance because every keystroke doesn't trigger a render event in every component. But it's not that simple to control the form submit with Formik. When you use, redux-saga, for instance, you won't be able to control the form from there. So here we are, dispatching actions now can trigger the Formik submit.

Getting Started

Prerequisites

Install

npm install --save formik-redux

Usage

In your root reducer, you must add our reducer:

import { reducer as formik } from "formik-redux"; // it must be formik the reducer name
import { combineReducers } from "redux";

import { otherReducer } from "./reducers";

export const appReducer = combineReducers({
    formik,
    otherReducer
});

HOC

In your form component, use the hoc:

import { withForm } from "formik-redux";

const DummyForm = props => <form onSubmit={ props.handleSubmit }></form>;

export default withForm({ form: "heygoodlooking" })( DummyForm );

You must pass the form prop. All the other props will be passed to Formik (see docs for more details).

To indicate to redux that the submit started or stopped, you must dispatch the appropriate action.

import { startSubmit, stopSubmit, withForm } from "formik-redux";
import { connect } from "react-redux";
import { compose } from "recompose";

const FORM_NAME = "myBeautifulForm";

const DummyForm = ({ handleSubmit, submitting }) => (
    <form onSubmit={ handleSubmit }>
        <button type="submit" disabled={ submitting }></button>
    </form>
);

async function onSubmit ( values, { props } ) {
    await props.dispatch( startSubmit( FORM_NAME ) );
    await props.dispatch( stopSubmit( FORM_NAME ) );
};

export default compose(
    connect( null, {
        startSubmit,
        stopSubmit
    }),
    withForm({
        form: FORM_NAME,
        handleSubmit: onSubmit
    })
)( DummyForm );

As you can see in the example above, is injected the prop submitting in the component to indicate if the submit finished or not.

Submit error

If there was some problem in the submit, to pass the error, you should use the second argument of the stopSubmit action.

const DummyForm = ({ handleSubmit, error, submitting }) => (
    <form onSubmit={ handleSubmit }>
        <button type="submit" disabled={ submitting }></button>
        { error && <span>{ error }</span> }
    </form>
);

async function onSubmit ( values, { props } ) {
    await props.dispatch( stopSubmit( FORM_NAME, "Something went wrong" ) );
};

In the component will be injected the prop error.

Hook

To use the hook, you must import useForm and pass the prop form.

import { useForm } from "formik-redux";

const FORM_NAME = "foobar";

const DummyForm = () => {
    const { error, submitting, ...formikProps } = useForm({
        form: FORM_NAME,
        // ...form props of Formik
    });
};

The hook will return the props error, submitting and the other formik props (see docs for more details).

Form

To use hooks with Formik Context you need to wrap your component.

import { Field } from "formik";
import { Form, useForm } from "formik-redux";

const DummyForm = () => {
    const formik = useForm({
        form: FORM_NAME,
        // ...form props of Formik
    });

    return (
        <Form formik={ formik }>
            <Field name="test" />
        </Form>
    );
};