My Day

Someday will be my day. Iwill have my own personal utopia, where all of my dreams, all of my desires, come true for the 24 hour span that is one day. Where disdain and harsh looks take a pause, where…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Functional Components vs Class Components in React

One of the first things you learn when starting React are the differences between class components and functional components. Here is a quick overview of the basics…

Class components have a few key syntax requirements. Each class component must have a render function as seen on line 11 of the code above. Another key syntax moment is the export default on line 23. React components require the export default in order to run properly.

The main attribute of class components is they allow for the use of state. In this example, I created an array of months that I would like to pass down to the Months component. In order to access the array, I called on {this.state.months} and pass it as props to the Months component.

A functional component is just a basic JavaScript function that takes on the argument of props. Since we passed the months array in the class component as months={this.state.months}, we can access this array in the functional component by using {props.months}. In the example above, I mapped through the array to create a list on the webpage.

To help provide a greater understanding, I converted the months functional component to a class component. The App class above can stay exactly the same. The main difference is we have to access the props slightly differently in a class component. Instead of just calling on {props.months} to get the array of months, we call on {this.props.months}.

Functional components are easy to understand for someone new to JavaScript since they are just normal functions that take on the argument props. Class components can utilize this and state to access props and other functions within the class.

Add a comment

Related posts:

Prediksi Master Angka Hongkong

HONGKONG JAM BUKA 23:03 PREDIKSI TANGGAL 22 JANUARI 2023 ‘’PREDIKSI HONGKONG MINGGU’’ TANGGAL 22–01–2023 *BBFS : 3 5 0 1 2 9 7 *AM : 7 2 5 0 *CB : 1 & 9 *TWIN : 55 / 99 / 33 *4D(BB) : 0732 2570 7502…

Words Can Create Life

Words can create that common life style.. “Words Can Create Life” is published by Jessica Milliner.

Indagine sugli adolescenti in Ogliastra

Le problematiche legate al disagio in età evolutiva sono in continuo cambiamento, come dimostra un’indagine sui comportamenti a rischio negli adolescenti in Ogliastra condotta dalla ASSL di Lanusei…