Skip to content

Commit

Permalink
Simple form with Redux Form (including validation).
Browse files Browse the repository at this point in the history
  • Loading branch information
roelfie committed Jan 7, 2021
1 parent a7db6a4 commit 4cfcbc7
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 7 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ My notes of the Udemy course 'Modern React with Redux'
- [Faker](https://github.com/marak/Faker.js/) (`basics` app)
- [Lodash](https://lodash.com/) (`blog` app)
- [Google API JavaScript client](https://github.com/google/google-api-javascript-client)
- JavaScript state managers
- [Redux](https://redux.js.org/) ([Thunk](https://github.com/reduxjs/redux-thunk))
- Redux / MobX
- [Redux](https://redux.js.org/)
- [Thunk](https://github.com/reduxjs/redux-thunk)
- [Redux Form](https://redux-form.com/)
- [Mobx](https://mobx.js.org/README.html)
- Browser APIs
- [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API) (`seasons` app)
Expand Down Expand Up @@ -1222,7 +1224,16 @@ Save data in the Redux store between page refreshes:
https://localhost:3000/?debug_session=SOME_KEY
```
By changing the session id in the URL you can even
By changing the session id in the URL you can even maintain multiple sessions (i.e. multiple Redux stores) at the same time.
# Section 23: Redux Forms
When using React with Redux you have to write a lot of boiler plate code:
- **mapStateToProps** to use data from the Redux store in your React components
- **Action creators** and **reducers** to store input data from your React components in the Redux store
When it comes to form elements, [Redux Form](https://redux-form.com/) simplifies this by providing standard mappers, action creators and reducers to automatically map data between the Redux store and form components.
# Appendix: JavaScript
Expand Down
33 changes: 33 additions & 0 deletions streams/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions streams/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"redux": "^4.0.5",
"redux-form": "^8.3.7",
"web-vitals": "^0.2.4"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion streams/client/src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Header = () => {
return (
<div className='ui secondary pointing menu'>
<Link to='/streams/new' className='item'>
Stream
New Stream
</Link>
<div className='right menu'>
<Link to='/' className='item'>
Expand Down
52 changes: 50 additions & 2 deletions streams/client/src/components/streams/StreamCreate.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
import React from "react";
import { Field, reduxForm } from "redux-form";

class StreamCreate extends React.Component {
renderError(error, touched) {
if (error && touched) {
return <div className='ui error message'>{error}</div>;
}
}

renderInput = (fieldProps) => {
// fieldProps.input & fieldProps.meta are provided by the redux-forms framrwork.
// Custom Field attributes (like label) are also made available on the fieldProps object.
const error = fieldProps.meta.error;
const touched = fieldProps.meta.touched;
const cssClass = error && touched ? "field error" : "field";
return (
<div className={cssClass}>
<label>{fieldProps.label}</label>
<input {...fieldProps.input} autoComplete='off' />
{this.renderError(error, touched)}
</div>
);
};

onSubmit(formValues) {
// Form is already stored in Redux store. At this point we can submit to backend server etc.
}

render() {
return <div>StreamCreate</div>;
return (
<form onSubmit={this.props.handleSubmit(this.onSubmit)} className='ui form error'>
<Field name='title' component={this.renderInput} label='Title' />
<Field name='description' component={this.renderInput} label='Description' />
<button className='ui button'>Submit</button>
</form>
);
}
}

export default StreamCreate;
const validate = (formValues) => {
const errors = {};
if (!formValues.title) {
errors.title = "Invalid title";
}
if (!formValues.description) {
errors.description = "Invalid description";
}
return errors;
};

export default reduxForm({
// Name to identify this component's form in the Redux store
form: "createStream",
// Reference to the form validation method
validate: validate
})(StreamCreate);
3 changes: 2 additions & 1 deletion streams/client/src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { combineReducers } from "redux";
import { reducer as formReducer } from "redux-form";
import { SIGN_IN, SIGN_OUT } from "../actions/types";

const SIGNED_OUT_STATE = {
Expand All @@ -24,4 +25,4 @@ const authenticationReducer = (state = SIGNED_OUT_STATE, action) => {
}
};

export default combineReducers({ authDetails: authenticationReducer });
export default combineReducers({ authDetails: authenticationReducer, form: formReducer });

0 comments on commit 4cfcbc7

Please sign in to comment.