What Are Components in ReactJS
What Are Components in ReactJS
What Are Components in ReactJS
Components are like pure javascript functions that help make the code easy by splitting the logic into
reusable independent code.
Components as functions
test.jsx
function Hello() {
We have created a function called Hello that returned h1 tag as shown above. The name of the
function acts as an element, as shown below:
The Component Hello is used as an Html tag, i.e., <Hello /> and assigned to Hello_comp variable and
the same is exported using export.
Index.js
ReactDOM.render(
Hello_comp,
document.getElementById('root')
);
Class as Component
test.jsx
render() {
index.js
ReactDOM.render(
<Hello />,
document.getElementById('root')
);
A state is a javascript object similar to props that have data to be used with the reactjs render. The
state data is a private object and is used within components inside a class.
test.jsx
constructor(props) {
super(props);
this.state = {
render() {
return <h1>{this.state.msg}</h1>;
index.js
ReactDOM.render(
<Hello />,
document.getElementById('root')
);
Props are properties to be used inside a component. They act as global object or variables which can
be used inside the Component.
function Hello(props) {
return <h1>{props.msg}</h1>;
index.js
ReactDOM.render(
Hello_comp,
document.getElementById('root')
);
test.jsx
render() {
return <h1>{this.props.msg}</h1>;
}
}
Index.js
ReactDOM.render(
document.getElementById('root')
);
A component life cycle is divided into Initialization, Mounting, Update, and UnMounting stages.
Here it will have the default props and the state at the initial level.
Mounting: In this phase, the Component is rendered inside the dom. We having exposure to
following methods in the mounting state.
• componentDidMount(): This is also called when the Component is just added to the dom.
• render(): You have this method for all the components created. It returns the Html node.
Update: In this state, the dom is interacted by a user and updated. For example, you enter something
in the textbox, so the state properties are updated.
UnMounting: this state comes into the picture when the Component is not required or removed.
Example: complife.jsx
constructor(props) {
super(props);
this.UpdateName = this.UpdateName.bind(this);
this.testclick = this.testclick.bind(this);
UpdateName(event) {
this.setState({name: event.target.value});
testclick(event) {
componentDidMount() {
shouldComponentUpdate() {
return true;
}
componentDidUpdate() {
componentWillUnmount() {
render() {
return (
<div>
<h2>{this.state.name}</h2>
</div>
);
index.js
ReactDOM.render(
<COMP_LIFE />,
document.getElementById('root')
);
In reactjs Html input elements like <input />, <textarea /> and <select /> has their own state and
needs to be updated when user interacts using the setState() method.
form.jsx
constructor(props) {
super(props);
this.UpdateName = this.UpdateName.bind(this);
this.formSubmit = this.formSubmit.bind(this);
UpdateName(event) {
this.setState({name: event.target.value});
formSubmit(event) {
render() {
return (
<form>
<h2>{this.state.name}</h2>
</form>
);
For the input fields, we need to maintain the state, so for that react provides a special method called
setState, which helps to maintain the state whenever there is a change.
We have used events onChange and onClick on the textbox and submit button. When the user enters
inside the textbox the onChange event is called, and the name field inside state object state is
updated as shown below:
UpdateName(event) {
this.setState({name: event.target.value});
index.js
ReactDOM.render(
<Form />,
document.getElementById('root')
);
Working with Events in ReactJS
Working with events in reactjs is same as how you would have done in javascript. You can use all the
event handlers that are used in javascript. The setState() method is used to update the state when
the user interacts with any Html element.
events.jsx
constructor(props) {
super(props);
this.UpdateName = this.UpdateName.bind(this);
this.testclick = this.testclick.bind(this);
UpdateName(event) {
this.setState({name: event.target.value});
testclick(event) {
render() {
return (
<div>
<h2>{this.state.name}</h2>
<input type="button" value="Click Here" onClick={this.testclick} />
</div>
);
For the input fields, we need to maintain the state, so for that react provides a special method called
setState, which helps to maintain the state whenever there is a change.
We have used events onChange and onClick on the textbox and button. When the user enters inside
the textbox the onChange event is called, and the name field inside state object state is updated as
shown below:
UpdateName(event) {
this.setState({name: event.target.value});
index.js
ReactDOM.render(
<EventTest />,
document.getElementById('root')
);
Will take a look at a working example to understand the working of inline css in reactjs.
addstyle.jsx
color: 'red'
};
function Hello(props) {
index.js
ReactDOM.render(
Hello_comp,
document.getElementById('root')
);
Let us create a external css , for that create a folder css/ and add style.css in it.
style.css
.h1tag {
color:red;
}
<!DOCTYPE html>
<html>
<head>
<title>ReactJS Demo</title>
</head>
<body>
<div id = "root"></div>
</body>
</html>
addstyle.jsx
function Hello(props) {
index.js
Hello_comp,
document.getElementById('root')
);