Skip to content

Instantly share code, notes, and snippets.

@shinaisan
Last active October 25, 2017 13:46
Show Gist options
  • Save shinaisan/5a257dce2d545bb20b9bf439a4ffe367 to your computer and use it in GitHub Desktop.
Save shinaisan/5a257dce2d545bb20b9bf439a4ffe367 to your computer and use it in GitHub Desktop.
Redux Form - Getting Started (with Bootstrap)

Redux Form - Getting Started

The original example is here: Redux Form - Getting Started

How to Run the Dev Server

./cra-script.sh
cd redux-form-getting-started
yarn run start
import React from 'react';
import ContactForm from './ContactForm';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import reducer from './reducers';
import "bootstrap/dist/css/bootstrap.css";
const store = createStore(reducer);
class App extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.setState({debug: ''});
}
submit(values) {
this.setState({debug: JSON.stringify(values)});
}
render() {
const submit = this.submit.bind(this);
return (
<Provider store={store}>
<div>
<ContactForm onSubmit={submit} />
<div>
<pre>{this.state.debug}</pre>
</div>
</div>
</Provider>
);
}
}
export default App;
import React from 'react';
import { Field, reduxForm } from 'redux-form';
import {
Form, FormGroup, FormControl,
InputGroup
} from 'react-bootstrap';
const renderField = ({ input, name, label, type }) => (
// https://redux-form.com/7.1.1/docs/api/field.md/
// > The props under the input key are what connects your input component
// > to Redux and are meant to be destructured into your
// > <input/> component.
<FormGroup>
<InputGroup>
<InputGroup.Addon>{label}</InputGroup.Addon>
<FormControl
{...input}
name={name}
placeholder={label}
type={type}
/>
</InputGroup>
</FormGroup>
);
let ContactForm = props => {
const { handleSubmit } = props
return (
<Form onSubmit={ handleSubmit }>
<Field name="firstName" label="First Name"
component={renderField} type="text" />
<Field name="lastName" label="Last Name"
component={renderField} type="text" />
<Field name="email" label="email"
component={renderField} type="email" />
<button type="submit">Submit</button>
</Form>
)
};
ContactForm = reduxForm({
// a unique name for the form
form: 'contact'
})(ContactForm);
export default ContactForm;
#!/bin/bash
NAME=redux-form-getting-started
echo -n "This script runs create-react-app $NAME. Proceed? (Y/N) "
read YN
if [ x$YN != xY ]
then
echo "Bye."
exit
fi
# Delete this line only if you are sure what is done from this line on.
exit
create-react-app $NAME
cd $NAME
cp -v ../package.json .
cd src
rm -f App.* logo.svg
cp -v ../../*.js .
cd ..
yarn install
echo 'To launch the dev server, run `yarn run start`.'
{
"name": "redux-form-getting-started",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^3.3.7",
"react": "^16.0.0",
"react-bootstrap": "^0.31.5",
"react-dom": "^16.0.0",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-form": "^7.1.1"
},
"devDependencies": {
"react-scripts": "1.0.14"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
const rootReducer = combineReducers({
// ...your other reducers here
// you have to pass formReducer under 'form' key,
// for custom keys look up the docs for 'getFormState'
form: formReducer
});
export default rootReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment