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 ...

First Repeating Element | Easy | Techgig

First Repeating Element | Easy | Techgig C++ Solution First Repeating Element | Easy | Techgig C++ Solution The first repeating element is the problem that comes under the Linear Search problem under the Algorithm section. Linear Search or sequential search is a method for finding an element within a list. The algorithm works by selecting and checking each number sequentially until matched. A linear search runs in at the worst linear time and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then the sequential search has an average case of (n+1)/2 comparisons, but the average case can be affected if the search probabilities for each element vary. The complexity of the linear search is as follows: The basic linear search algorithm has the following steps: Given a list L on n elements with values L0…Ln-1, and target value T, to find the index of the target T in the list L. Set i to 0. If Li = T , the search te...

Data Scraping with Python 2020

Are you a regular coursera user? If yes, then you might like this python program. You can actually use the same program for similar course websites like edx, alison, udemy, etc. So, let’s start. If you have some experience with python and BeautifulSoup then you already have everything you need so make your own. The ones that don’t, follow along. Install python for your operating system. After that install requests and BeautifulSoup like this: After that, lets import the modules, and make a variable to store the url: from bs4 import BeautifulSoup import requests baseUrl = “https://www.coursera.org” Now, take an input from cli or initialize it: skillset = input().split(“ “)  Now, we’ll see the query url for the search input: example: java Fig.1. Search field in a course website Fig.2. Check the url and find a pattern So, the important part is the after the “query=” part. So, we will append the input from the user here. skillset= “%20”.join(input().split(“ “)) courseraUrl = “https://...