- React - Home
- React - Introduction
- React - Roadmap
- React - Installation
- React - Features
- React - Advantages & Disadvantages
- React - Architecture
- React - Creating a React Application
- React - JSX
- React - Components
- React - Nested Components
- React - Using Newly Created Components
- React - Component Collection
- React - Styling
- React - Properties (props)
- React - Creating Components using Properties
- React - props Validation
- React - Constructor
- React - Component Life Cycle
- React - Event management
- React - Creating an Event−Aware Component
- React - Introduce Events in Expense Manager APP
- React - State Management
- React - State Management API
- React - Stateless Component
- React - State Management Using React Hooks
- React - Component Life Cycle Using React Hooks
- React - Layout Component
- React - Pagination
- React - Material UI
- React - Http Server
- React - Http client programming
- React - Form Programming
- React - Forms
- React - Controlled Component
- React - Uncontrolled Component
- React - Formik
- React - Conditional Rendering
- React - Lists
- React - Keys
- React - Routing
- React - Redux
- React - Animation
- React - Bootstrap
- React - Map
- React - Table
- React - Managing State Using Flux
- React - Testing
- React - CLI Commands
- React - Building and Deployment
- React - Example
- Hooks
- React - Introduction to Hooks
- React - Using useState
- React - Using useEffect
- React - Using useContext
- React - Using useRef
- React - Using useReducer
- React - Using useCallback
- React - Using useMemo
- React - Custom Hooks
- React Advanced
- React - Accessibility
- React - Code Splitting
- React - Context
- React - Error Boundaries
- React - Forwarding Refs
- React - Fragments
- React - Higher Order Components
- React - Integrating With Other Libraries
- React - Optimizing Performance
- React - Profiler API
- React - Portals
- React - React Without ES6 ECMAScript
- React - React Without JSX
- React - Reconciliation
- React - Refs and the DOM
- React - Render Props
- React - Static Type Checking
- React - Strict Mode
- React - Web Components
- Additional Concepts
- React - Date Picker
- React - Helmet
- React - Inline Style
- React - PropTypes
- React - BrowserRouter
- React - DOM
- React - Carousel
- React - Icons
- React - Form Components
- React - Reference API
- React Useful Resources
- React - Quick Guide
- React - Cheatsheet
- React - Axios CheatSheet
- React - Useful Resources
- React - Discussion
React - Add Expense
Open ExpenseEntryItemList.js and import connect from redux library.
import { connect } from 'react-redux';
Next, import Formik library..
iimport { Formik } from 'formik';
Next, import useNavigate method from router library.
import { useNavigate } from "react-router-dom";
Next, import addExpense from our action library.
import { addExpense } from '../actions/expenseActions';
Next, create constructor with initial values for expenses.
constructor(props) {
super(props);
this.initialValues = { name: '', amount: '', spend_date: '', category: '' }
}
Next, write the validate method.
validate = (values) => {
const errors = {};
if (!values.name) {
errors.name = 'Required';
}
if (!values.amount) {
errors.amount = 'Required';
}
if (!values.spend_date) {
errors.spend_date = 'Required';
}
if (!values.category) {
errors.category = 'Required';
}
return errors;
}
Next, add event handler method.
handleSubmit = (values, setSubmitting) =< {
setTimeout(() =< {
let newItem = {
name: values.name,
amount: values.amount,
spendDate: values.spend_date,
category: values.category
}
this.props.addExpense(newItem);
setSubmitting(false);
this.props.history.push("/list");
}, 400);
}
Here,
- Used addExpense method to add expense item
- Use router history method to move to expense list page.
Next, update render method with form created using Formik library.
render() {
return (
<div id="expenseForm">
<Formik
initialValues={this.initialValues}
validate={values => this.validate(values)}
onSubmit={(values, { setSubmitting }) => this.handleSubmit(values, setSubmitting)}>
{
({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
/* and other goodies */
}) => (
<form onSubmit={handleSubmit}>
<label for="name">Title <span>{errors.name && touched.name && errors.name}</span></label>
<input type="text" id="name" name="name" placeholder="Enter expense title"
onChange={handleChange}
onBlur={handleBlur}
value={values.name} />
<label for="amount">Amount <span>{errors.amount && touched.amount && errors.amount}</span></label>
<input type="number" id="amount" name="amount" placeholder="Enter expense amount"
onChange={handleChange}
onBlur={handleBlur}
value={values.amount} />
<label for="spend_date">Spend Date <span>{errors.spend_date && touched.spend_date && errors.spend_date}</span></label>
<input type="date" id="spend_date" name="spend_date" placeholder="Enter date"
onChange={handleChange}
onBlur={handleBlur}
value={values.spend_date} />
<label for="category">Category <span>{errors.category && touched.category && errors.category}</span></label>
<select id="category" name="category"
onChange={handleChange}
onBlur={handleBlur}
value={values.category}>
<option value="">Select</option>
<option value="Food">Food</option>
<option value="Entertainment">Entertainment</option>
<option value="Academic">Academic</option>
</select>
<input type="submit" value="Submit" disabled={isSubmitting} />
</form>
)
}
</Formik>
</div>
)
}
Next, map dispatch method to component properties.
const mapDispatchToProps = {
addExpense,
};
Finally, connect the component to store.
export default connect( null, mapDispatchToProps )(ExpenseEntryItemForm);
The complete source code of the component is as follows −
import React from "react";
import { connect } from 'react-redux';
import { Formik } from 'formik';
import { useNavigate } from "react-router-dom";
import { addExpense } from '../actions/expenseActions';
class ExpenseEntryItemForm extends React.Component {
constructor(props) {
super(props);
this.initialValues = { name: '', amount: '', spend_date: '', category: '' }
}
validate = (values) => {
const errors = {};
if (!values.name) {
errors.name = 'Required';
}
if (!values.amount) {
errors.amount = 'Required';
}
if (!values.spend_date) {
errors.spend_date = 'Required';
}
if (!values.category) {
errors.category = 'Required';
}
return errors;
}
handleSubmit = (values, setSubmitting) => {
setTimeout(() => {
const navigate = useNavigate();
let newItem = {
name: values.name,
amount: values.amount,
spendDate: values.spend_date,
category: values.category
}
this.props.addExpense(newItem);
setSubmitting(false);
navigate("/list");
}, 400);
}
render() {
return (
<div id="expenseForm">
<Formik
initialValues={this.initialValues}
validate={values => this.validate(values)}
onSubmit={(values, { setSubmitting }) => this.handleSubmit(values, setSubmitting)}>
{
({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
/* and other goodies */
}) => (
<form onSubmit={handleSubmit}>
<label for="name">Title <span>{errors.name && touched.name && errors.name}</span></label>
<input type="text" id="name" name="name" placeholder="Enter expense title"
onChange={handleChange}
onBlur={handleBlur}
value={values.name} />
<label for="amount">Amount <span>{errors.amount && touched.amount && errors.amount}</span></label>
<input type="number" id="amount" name="amount" placeholder="Enter expense amount"
onChange={handleChange}
onBlur={handleBlur}
value={values.amount} />
<label for="spend_date">Spend Date <span>{errors.spend_date && touched.spend_date && errors.spend_date}</span></label>
<input type="date" id="spend_date" name="spend_date" placeholder="Enter date"
onChange={handleChange}
onBlur={handleBlur}
value={values.spend_date} />
<label for="category">Category <span>{errors.category && touched.category && errors.category}</span></label>
<select id="category" name="category"
onChange={handleChange}
onBlur={handleBlur}
value={values.category}>
<option value="">Select</option>
<option value="Food">Food</option>
<option value="Entertainment">Entertainment</option>
<option value="Academic">Academic</option>
</select>
<input type="submit" value="Submit" disabled={isSubmitting} />
</form>
)
}
</Formik>
</div>
)
}
}
const mapDispatchToProps = {
addExpense,
};
export default connect(
null,
mapDispatchToProps
)(ExpenseEntryItemForm);
Next, serve the application using npm command.
npm start
Next, open the browser and enter http://localhost:3000 in the address bar and press enter.
Finally, we have successfully created a simple react application with basic features.
Conclusion
React is one of the most popular and highly recommended UI frameworks. True to its popularity, it is being developed for a very long time and actively maintained. Learning react framework is a good starting point for the front end developers and will surely help them to improve their professional career.