Repository navigation
Dynamic forms
- The form’s structure is similar to those of the usual HTML forms. However, each input element gets a component of its own, which we call dumb components. The container component is responsible for maintaining the state. The difference is, we're using a callback function to handle form events and then using the container’s state to store the form data.
- Because we are using the value attribute on the form element, the value displayed will be the value of this.state.value.
- There is another technique, popularly known as uncontrolled components, for creating input forms. This is more like traditional HTML forms because the input form data is stored inside the DOM and not within the component. Elements like and <textarea> maintain their own state, which they update when the input values change. You can query the DOM for the value of an input field using a ref.
class NameForm extends Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e) {
alert('The value is: ' + this.input.value);
e.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" ref={(input) => this.input = input} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
React recommends using controlled components over refs to implement forms. Refs offer a backdoor to the DOM, which might tempt you to use it to do things the jQuery way. Controlled components, on the other hand, are more straightforward — the form data is handled by a React component.
import React, { Component } from 'react';
import './styles.css';
import FormContainer from './containers/FormContainer';
class App extends Component {
render() {
return (
<div className="container">
<h3>React Form</h3>
<FormContainer />
</div>
);
}
}
export default App;
- FormContainer is a container component that renders all of the form elements and handles all of the business logic. We call it a container component because it takes care of updating the state of the form, handling form submission, and making API calls/dispatching Redux actions.
- The dumb components or presentational components are concerned with how things look and contain the actual DOM markup.These components receive data and callbacks exclusively as props.
import React, { Component } from 'react';
import './styles.css';
import FormContainer from './containers/FormContainer';
class App extends Component {
render() {
return (
export default App; FormContainer is a container component that renders all of the form elements and handles all of the business logic. We call it a container component because it takes care of updating the state of the form, handling form submission, and making API calls/dispatching Redux actions. The dumb components or presentational components are concerned with how things look and contain the actual DOM markup. These components receive data and callbacks exclusively as props. I've covered more about it on my Stateful vs. Stateless components in React tutorial.
Let's move on and create a container:
import React, {Component} from 'react';
/* Import Components */
import CheckBox from '../components/CheckBox';
import Input from '../components/Input';
import TextArea from '../components/TextArea';
import Select from '../components/Select';
import Button from '../components/Button'
class FormContainer extends Component {
constructor(props) {
super(props);
this.state = {
newUser: {
name: '',
email: '',
age: '',
gender: '',
expertise: '',
about: ''
},
genderOptions: ['Male', 'Female', 'Others'],
skillOptions: ['Programming', 'Development', 'Design', 'Testing']
}
this.handleFormSubmit = this.handleFormSubmit.bind(this);
this.handleClearForm = this.handleClearForm.bind(this);
}
/* This life cycle hook gets executed when the component mounts */
handleFormSubmit() {
// Form submission logic
}
handleClearForm() {
// Logic for resetting the form
}
render() {
return (
<form className="container" onSubmit={this.handleFormSubmit}>
<Input /> {/* Name of the user */}
<Input /> {/* Input for Age */}
<Select /> {/* Gender Selection */}
<CheckBox /> {/* List of Skills (eg. Programmer, developer) */}
<TextArea /> {/* About you */}
<Button /> { /*Submit */ }
<Button /> {/* Clear the form */}
</form>
);
}
}
export default FormContainer;
}
input component
const Input = (props) => {
return (
<div className="form-group">
<label htmlFor={props.name} className="form-label">{props.title}</label>
<input
className="form-input"
id={props.name}
name={props.name}
type={props.type}
value={props.value}
onChange={props.handleChange}
placeholder={props.placeholder}
/>
</div>
)
}
export default Input;