Code Monkey home page Code Monkey logo

class-components's Introduction

Mastering React Class Components

These are my personal notes on mastering React's legacy Class components. I'm using Scrimba's Class Components in React tutorial by Bob Ziroll as a reference.

Find it HERE on Scrimba or HERE on YouTube.

Declaring a Class component vs declaring a Function component:

  • Function component:
import React from 'react';

const App = () => {
	return <div>App</div>;
};

export default App;
  • Class component: You can either destructure Component from the React class like this:
import React, { Component } from 'react';

export default class App extends Component {
	render() {
		return <div> Function component</div>;
	}
}

Or declare it like this without destructuring:

import React from 'react';

export default class App extends React.Component {
	render() {
		return <h1> Class component</h1>;
	}
}

Passing props in Class Components vs Function components

  • Function Components It has to be accepted as a parameter to the function then used.
// main.jsx / index.js
<App type="Function" />;

// App.jsx
import React from 'react';

const App = ({ type }) => {
	return <div>{type} Component</div>;
};

export default App;
  • Class Components You don't have to accept the prop as an argument. The class automatically add it to any component created with that name. Just pass this.props.<the_prop_name> into the component.
// main.jsx / index.js
<App type="Class" />;

// App.jsx
import React from 'react';

export default class App extends React.Component {
	render() {
		return <h1>{this.props.type} component</h1>;
	}
}

Declaring state in Function components vs in Class components

  • Function Component State in function components can be of any value; booleans, a string, an array, a number etc.
import { useState } from 'react';

const State = () => {
	const [goOut, setGoOut] = useState('Yes');

	return <div>{/* Rest of the component */}</div>;
};

export default State;
  • Class component State in class components are always objects and every data we add will be a property on that object

    The object MUST also be called state.

import React, { Component } from 'react';

export class State extends Component {
	// This is how to declare a state in class components
	state = {
		goOut: 'Yes',
	};

	toggleGoOut() {
	// The function for changing state goes in here
	}
	render() {
		return (
			// Rest of the component
		)
	}
}

export default State;

Declaring a state setter function in Class components:

The example above on setting state in Function components covers the state variable, initial state value, and state setter in one line of code. In class components, things are a it different.

The state setter is inside the Component class er extend from React. To use it:

  1. Make sure your class method is an arrow function:
// WRONG
toggleGoOut() {
	// The method for changing state goes in here
	}

// CORRECT
toggleGoOut = () => {
	// The method for changing state goes in here
	}
  1. The setter method for changing state is called setState in class components. Call it inside your method and pass the new state into it either as a value or as an arrow function.
toggleGoOut = () => {
	this.setState((prevState) => {
		return {
			goOut: prevState.goOut === 'Yes' ? 'No' : 'Yes',
		};
	});
};

More notes inside the State.jsx component in the /src folder.

Lifecycle Methods

  • Mounting phase => render() and componentDidMount()
  • Updating phase => render() and componentDidUpdate()
  • Unmounting phase => componentWillUnmount()

i. render() method

  • Is used to "paint" elements on the screen.
  • Called when components first mount or when props change.

ii. componentDidMount() method

  • Added to class-based components and whatever code is put inside it is code that will run immediately after the very first render of the component.
  • Runs every refresh too.
import React from 'react';

export default class App extends React.Component {
	componentDidMount() {
		console.log('componentDidMount');
	}

	render() {
		console.log('render');
		return <h1>Hello</h1>;
	}
}

/*

Console:
$ render
$ componentDidMount

// render always runs first then the component mounts.
*/

Works like useEffect

class-components's People

Contributors

oyieroyier avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.