Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Redux-Form with React-Bootstrap?

Tags:

redux-form

I am trying to use "redux-form": "^6.7.0" with "react-bootstrap": "^0.31.0"

My Component renders nicely, but when I press Submit, what I see is an empty object.

note: I have tried wrapping the Config with connect first, and as show below, first wraping it with redux-form and then with the from react-redux connect()

Configuration.js

class Config extends Component {
    render() {
        const { ServerPort, UserID, PortNumber, WWWUrl, SourcePath, FMEPath, pickFile, pickFolder, handleSubmit } = this.props;
        return (
            <Form horizontal onSubmit={handleSubmit}>
                <FormGroup controlId="serverPortBox">
                    <Col componentClass={ControlLabel} sm={2}>Watson Port:</Col>
                    <Col sm={10}>
                        <OverlayTrigger placement="left" overlay={(<Tooltip id="tt1">TCP port for Watson to use</Tooltip>)}>
                            <Field name="WatsonPort" component={FormControl}
                                type="number" min="1024" max="65535" placeholder={ServerPort} />
                        </OverlayTrigger>
                    </Col>
                </FormGroup>

 ......

const CForm = reduxForm({
    form: 'configuration' // a unique name for this form
})(Config);

const Configuration = connect(mapStateToProps, mapDispatchToProps)(CForm)

export default Configuration

reducers.js

import { combineReducers } from 'redux'
import { reducer as formReducer } from 'redux-form

......

const reducerList = {
    GLVersion,
    ServerConfig,
    ServerStats,
    form: formReducer
}

export default combineReducers(reducerList)

Main Package Dashboard.js

what i see in the debugger is that config is an empty object

    <Panel className='configPanel'
      collapsible header="Configuration"
      eventKey="1"
      defaultExpanded={true}>
      <Configuration onSubmit={(config) => writeConfig(config)} />
    </Panel>
like image 279
Dr.YSG Avatar asked May 11 '17 19:05

Dr.YSG


People also ask

How do I use forms in Bootstrap React?

If you look in your browser window, it should just say "Form". import React, { Component } from 'react'; import Container from 'react-bootstrap/Container'; import Form from 'react-bootstrap/Form'; class SimpleForm extends Component { render() { return ( <Container> <Form> <Form. Group controlId="formName"> <Form.

Is it good to use Bootstrap With React?

React-Bootstrap replaces the Bootstrap JavaScript. Each component has been built from scratch as a true React component, without unneeded dependencies like jQuery. As one of the oldest React libraries, React-Bootstrap has evolved and grown alongside React, making it an excellent choice as your UI foundation.

How install bootstrap5 in react JS?

Installing BootstrapDownload the source files and run it locally, Download the minified CSS and JS files and drop them directly into your project. Use the CDN (this approach is not very customizable) Use package managers (it is published as a pre-release to npm, yarn, RubyGems, etc.)

How to use Redux with react?

2. Installing Redux in React First, you need to install Redux in your app. Run the command: In the second step, you need to install React-Redux. This package allows your React component to have access to the store. 3. Creating the store After installing the redux dependencies, we can create our store.

What is react-bootstrap?

React-Bootstrap is the most popular front-end framework. It’s rebuilt for React.js—that is, it completely replaces Bootstrap Javascript. React-Bootstrap is an npm package that completely re-implements Bootstrap components for React and has no dependency on JQuery.

How do I create a react app in Visual Studio Code?

Create your react application The first step is to create a React application (a simple to-do-list). I’m using VS Code but you can stick to your favorite code editor. Open your terminal and type: This commands creates your React application in a folder called react-with-redux. Open that folder with your code editor. (cd react-with-redux).

How do I create a redux store from an existing store?

After installing the redux dependencies, we can create our store. You do this in your index.js file in the src folder. First, we import the createStore function from ‘redux’ (line 7 in the screenshot). This function creates the Redux store that holds the state.


Video Answer


2 Answers

See: https://github.com/erikras/redux-form/issues/2917

Oh, this was a great mystery. I followed the advice in https://github.com/react-bootstrap/react-bootstrap/issues/2210 and both the warning about additional props and the empty submit stopped.

It seems you have to wrap the Bootstrap in your custom component (why?, I don't know). Also make sure you custom component is a stateless funcitonal component, or after the first key press, you field will blur and lose focus.

There are some warnings in the documentation of redux-form about this.

my custom field component FieldInput

  const FieldInput = ({ input, meta, type, placeholder, min, max }) => {
        return (
            <FormControl
                type={type}
                placeholder={placeholder}
                min={min}
                max={max}
                value={input.value}
                onChange={input.onChange} />
        )
    }

and I invoke it like this:

<Field name="ServerPort"
   type='number' 
   component={FieldInput} 
   placeholder={ServerPort} 
   min="1024" max="65535" 
/>

see also: https://github.com/erikras/redux-form/issues/1750

So now, the definition of FieldInput and Config look like this:

import React, { Component } from 'react'
import { Field, reduxForm } from 'redux-form'
import { connect } from 'react-redux'
import { Form, FormControl, FormGroup, ControlLabel, Col, Button, Tooltip, OverlayTrigger } from 'react-bootstrap'
import * as Act from '../dash/actions.js'
import FaFolderOpen from 'react-icons/lib/fa/folder-open'
import FaFileCodeO from 'react-icons/lib/fa/file-code-o'

const FieldInput = ({ input, meta, type, placeholder, min, max }) => {
    return (
        <FormControl
            type={type}
            placeholder={placeholder}
            min={min}
            max={max}
            value={input.value}
            onChange={input.onChange} />
    )
}

const Config = ({ ServerPort, UserID, PortNumber, WWWUrl, SourcePath, FMEPath, pickFile, pickFolder, handleSubmit }) => {
    return (
        <Form horizontal onSubmit={handleSubmit}>
            <FormGroup controlId="serverPortBox">
                <Col componentClass={ControlLabel} sm={2}>Watson Port:</Col>
                <Col sm={10}>
                    <OverlayTrigger placement="left" overlay={(<Tooltip id="tt1">TCP port for Watson to use</Tooltip>)}>
                        <Field name="ServerPort" type='number' min="1024" max="65535" component={FieldInput} placeholder={ServerPort}  />
                    </OverlayTrigger>
                </Col>
            </FormGroup>
like image 104
Dr.YSG Avatar answered Nov 09 '22 01:11

Dr.YSG


Some props required by <FormControl> are passed inside props.input from <Field>, see http://redux-form.com/6.6.3/docs/api/Field.md/#props

To pass all those props in a generic way, instead of doing it explicitly, you can use the following function:

const ReduxFormControl = ({input, meta, ...props}) => {
     return <FormControl {...props} {...input} />
};

and then inside the form:

<Field component={ReduxFormControl} ... />

This way, value, onChange, etc. are all passed as expected to <FormControl>.

like image 37
Rüdiger Schulz Avatar answered Nov 08 '22 23:11

Rüdiger Schulz