• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Alex Aitken

Technical and Engineering Leadership, Coaching, and Mentorship

Using React-Redux-Forms

February 26, 2018 By Alex Leave a Comment

The main reason you could say we moved to Redux was validation. We didn’t want to add a whole lot of code to our page components to validate every child component. In some components, we had up to ten fields, and all of them had to be validated. Can you imagine the size of the component that validated all of these? Hundreds of lines long. For some of you, that’ll be okay. I mean, why can’t a component be long? For us, it’s about maintainability and having a large component is hard to navigate and see what it looks like.

Along came React-Redux-Forms

From the previous posts, you might’ve seen that we use Bootstrap within our React. And that’s true. We needed a way to be able to validate the Bootstrap components while not adding too much code. React-Redux-Form makes that pretty easy. It’s just a wrapper around your component (in this case the Bootstrap components) that encapsulate all the things you need when validating. It’ll handle all the stuff like dirty, touched etc. for you.

I’ve created a really basic example for you below. You must always surround your forms with a form tag. This is important as that’s where you’ll get submit etc. from. Firstly, I’ve created a component that is three buttons. Two create valid values, and we want the third to trigger an invalid value.

import * as React from 'react';
import { Button } from 'react-bootstrap';
 
interface MyProps {
    onChange: (name: string) => void;
    value: string;
}
 
export const MyComponent: React.StatelessComponent<MyProps> = (props) => {
    
    const onButtonClickJohn = () => {
        props.onChange('John');
    };
    
    const onButtonClickJacob = () => {
        props.onChange('Jacob');
    };
    
    const onButtonClickMichael = () => {
        props.onChange('Michael');
    };
    
    return (
        <div>
           <span>{props.value}</span>
           <Button onClick={onButtonClickJohn}>John</Button>
           <Button onClick={onButtonClickJacob}>Jacob</Button>
           <Button onClick={onButtonClickMichael}>Michael</Button>
        </div>
    );
};

Here’s the component in a form.

import * as React from 'react';
import MyComponent from './MyComponent';
import { Control, Form } from 'react-redux-form';

export interface MyComponentFormProps {
    viewModel: {
        myComponent: string;
    };
}

export default class MyComponentForm extends React.Component<MyComponentFormProps, {}>{
    constructor(props: MyComponentFormProps) {
        super(props);
    }
    
    handleSubmit() {
        console.log('submitted');
    }
    
    render() {
        const mapProps = {
            value: (props: MapPropsProps) => props.viewValue,
        };
        const validators = () => ({
            isNotMichael: (val: any): boolean => {
                if (val === 'Michael') {
                    return false;
                }
                return true;
            },
        });
    
        return (
            <Form className="myComponentForm" model="myComponentForm" onSubmit={this.handleSubmit}>
                <Control.text
                    component={MyComponent}
                    model={'myComponentForm.myComponent'}
                    mapProps={mapProps}
                    validators={validators}
                    updateOn="change"
                />
                <Errors
                  model="myComponentForm.myComponent"
                  messages={{
                    isNotMichael: 'Please don't choose michael.',
                  }}
                />
            </Form>
        );
    }
}

You need to add a little something to your reducers. There’s now a form part – if you need to access valid/invalid etc.

import React from 'react';
import { createStore } from 'redux';
import { combineForms } from 'react-redux-form';

const initialMyComponentForm = { myComponent: '' };

const store = createStore(combineForms({
  myComponentForm: initialMyComponentForm,
}));

Yours won’t be so easy

The above example is a relatively benign example of how you might introduce React-Redux-Forms to your React-Redux code. I also haven’t tested if it compiles, so let me know if it’s working. Unfortunately, life is not so easy. What you’ll find is that your forms will become more complicated. You’ll have components that need to be reused. You’ll have validators that need to be reused. Everything should be tested and be testable. And, to top it all off, you’ll probably want clean code that doesn’t pollute your components with validation logic or all this form stuff.

Reposted on Medium.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Like this:

Like Loading...

Related

Filed Under: Coding

Alex

Reader Interactions

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

About the author

Alex is an AVP of Engineering currently working at Bukalapak. He is a leader in full-stack technologies. Read More…

Pages

  • Speaking Experience
  • About

Social Profiles

  • LinkedIn
  • Medium
  • ADPList

Recent Posts

  • Interviewing as an Engineering Leader
  • Managing Low Performers
  • Getting Docker, React, .NET Core, Postgres, and Nginx Playing Nice
  • What Makes a Good Software Engineering Manager?
  • “Am I There Yet?” Said an Engineer

Archives

  • January 2025
  • August 2024
  • July 2024
  • October 2023
  • August 2023
  • October 2020
  • May 2020
  • February 2020
  • June 2019
  • March 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018

Categories

  • Coding
  • Essay
  • Leadership
  • Management
  • Roundtable
  • Strategy

Footer

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright © 2025

%d