Skip to main content

React JS Examples must do for a Beginner

If you are just starting react js or looking for a way to start react js , check out these examples below:

These examples / concepts are must for a beginner to get well versed with.

Stateful Components are components depending on it’s state object and change it’s own state. The component re-renders based on changes to it’s state, and may pass down properties of it’s state to child components as properties on a props object.

Stateful Components
Fig.1 Stateful Components

Stateless Components purely acts as a view, the components do not have any state. Here, we pass props (method argument) and provide the value along with it. Notice there is “this” keyword.

Fig.2. Stateless component

Implicit Return Components are the same as above but without a return statement. This would be useful when you are creating a button or a default form that could be used else where.

Fig.3. Implicit React Component

Fragments are used to render multiple elements without using wrapper class. Fragments let you group a list of children without adding extra nodes to the DOM. Fragments are used in place of necessary <div> tag so as to avoid invalid html DOM elements. You can use <> </> instead of <React.Fragment></React.Fragment>

Fig.4. Fragment in React

Normally you would use <div> </div> which makes the HTML rendering illogical and invalid due to <td> element ( also applicable to any listing element like <li>, etc. ).

JSX allows to write HTML elements in JS and place them in the DOM without any createElement() or appendChild() methods. It is used to convert HTML tags into react elements.

Fig.5. JSX element

Only single parent tag is allowed which means that no multiple parent tags are allowed, either nest it in <div> , <ul>, <table> or use React.Fragments.

Fig.6. Single parent tag allowed

One tip before we jump to our last example:

Fig.7. One last tip

You can see the value is updated to 2 after the component has been mounted, there can occur several problems with this method, use this instead:

Fig.8. Use prevState to fetch the previous state and then update it.

There exists a clearer way also:

State vs Props

State belongs to a component and is mutable which means the state of the component can be changed. The state of the component is changed using setState({}) method.

Props similar to a method argument, they are passed to a function or a constructor. The props that are passed are immutable.

Comments

Popular posts from this blog

How to use Chess com API using Python

  How to use Chess com API using Python Chess is an amazing strategy-based game and if you have been following the recent boom of online chess then welcome to the chess club. Online chess is amazing since it allows you to play with a random stranger at your level or stockfish (computer). There are many popular online chess websites like lichess.org, chess.com, playchess.com, and newly created kasparovchess.com. Today we will be seeing how to use chess.com API for getting players' stats. You can create software and get affiliates from them (check out the link below), so share it with them if you are planning to create something. Before you start make sure you have the following things: Pre-requirements Postman Anaconda or mini conda or Python idle Any text editor of your choice Pretty good? Now let’s download the JSON file that chess com developers have already made for us from here and then you import it to the Postman. This just helps you with prewritten get methods so that ...

Create your own YouTube video and playlist downloader using QT designer in Python

Create your own YouTube video and playlist downloader using QT designer in Python The code for this software is available on my GitHub, the link is given below. Create your own YouTube video and playlist downloader using QT designer in Python This is the thing we are trying to make today for this article The code for this software is available on my GitHub, the link is given below. If you haven’t checked out my previous article on the same topic but not the GUI version, please check it out. 3 ways to download YouTube playlists at once using Python Have you guys ever wanted to download your entire youtube playlist? rahulbhatt1899.medium.com So before starting to code let's check if you have all the requirements beforehand: Pre-requirements : Qt designer Anaconda or mini conda or python 3.7 compiler Any text editor of your choice Libraries to download: Pytube Ffmpy PyQt5 This would be sectioned into the following structure: Creating the UI in Qt designer Converting UI file int...

Authentication Using Passport JS

Authentication is big and most complicated part of an application, but it is also the  preponderance  part of any web app/software in general. Passport JS Passport JS is a widely popular authentication module for  Node js.  The sole purpose is to authenticate requests and is based on the idea of pluggable authentication strategies ( including local strategy, there are more than 500 strategies currently  available  ). When using third party application Your app never receives your password  thus freeing the developer from the burden of security related to handling and storing passwords. The Passport authentication and its strategy will include protection against attacks like “man in the middle” and other vectors an attacker might exploit. We are going to use Facebook strategy, for now, to install Passport and Facebook strategy type in the following command: Next, we are going to write the authentication code, and we’ll be creating a different module cal...