https://www.kirupa.com/react/components.htm
Concept
This is terrible coding because you are doing a lot of repetitive coding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var speed = 10; var time = 5; alert(speed * time); var speed1 = 85; var time1 = 1.5; alert(speed1 * time1); var speed2 = 12; var time2 = 9; alert(speed2 * time2); var speed3 = 42; var time3 = 21; alert(speed3 * time3); |
thus, functions were created:
1 2 3 4 |
function getDistance(speed, time) { var result = speed * time; alert(result); } |
…and therefore, we can easily call the functions and provide parameters:
1 2 3 4 |
getDistance(10, 5); getDistance(85, 1.5); getDistance(12, 9); getDistance(42, 21); |
How it Works in React
Now, in React, say we want to render a bunch of h1. We have a index.html template with a given div for us to inject html content into like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="theme-color" content="#000000"> <!-- manifest.json provides metadata used when your web app is added to the homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/ --> <link rel="manifest" href="%PUBLIC_URL%/manifest.json"> <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> <title>React App</title> </head> <body> <noscript> You need to enable JavaScript to run this app. </noscript> <div id="root"></div> </body> </html> |
Then, in index.js we use React’s API to render some custom html:
1 2 3 4 5 6 7 8 9 |
ReactDOM.render( <div> <h1>Batman</h1> <h1>Iron Man</h1> <h1>Nicolas Cage</h1> <h1>Mega Man</h1> </div>, destination ); |
this repetitive usage of the h1s is the equivalent of writing the same code over and over again. Because what if we want to change them to h3. Or add bold to it. Its tiring to change it for everyone one of them. Conceptually, we want to do something similar to as how functions have solved repetitive code.
React Components
React components are reusable chunks of JavaScript that output (via JSX) HTML elements
This is where we use JSX to generate and render html to throw into a specific div. In our case, it would be the id=root div.
You create a class that extends the features of component. We do so like this:
1 2 3 |
class HelloWorld extends React.Component { } |
This HelloWorld component is a component because it extends React.Component. If it didn’t do that, it would just be an empty class that doesn’t do much.
Inside our class, you can put all sorts of methods to further define what HelloWorld does.
However, there is mandatory function called Render which you must implement.
1 2 3 4 5 |
class HelloWorld extends React.Component { render() { return <p>Hello, componentized world!</p> } } |
1 2 3 4 |
ReactDOM.render( <HelloWorld/>, document.querySelector("#container") ); |
What we’ve done so far might seem crazy, but simply think of your
Properties
Our Hello component isn’t very useful because it only says Hello to one thing. It cannot say hello to other people/things. Say we want to have the Hello be applied to different names.
Just like with functions, you can pass in arguments that alter what your component does. There is a slight terminology update you need to be on top of. What we call arguments in the function world are going to be known as properties in the component world. Let’s see these properties in action!
Right now, our HelloWorld component is hard-coded to always send out Hello, world! as part of its return value. The first thing we are going to do is change that behavior by having return print out the value passed in by a property.
We are going to modify our HelloWorld component to allow you to specify who or what you greet besides the generic World.
Adding and accessing the property
We add the property by simply accessing it. If it doesn’t exist, it will be automatically added to the class. If it exists, it will access it.
The way you access a property is by referencing it via the this.props property that every component has access to. Notice how we specify this property. We place it inside curly brackets – { and }.
1 2 3 4 5 |
class HelloWorld extends React.Component { render() { return <p>Hello, {this.props.greetings} !! </p> } } |
In JSX, if you want something to get evaluated as an expression, you need to wrap that something inside curly brackets. If you don’t do that, you’ll see the raw text this.props.greetings printed out.
Making the component call
1 2 3 4 5 6 7 8 |
ReactDOM.render( <div id="row"> <HelloWorld greetings="Batman" /> <HelloWorld greetings="Superman" /> </div>, document.getElementById('root')); registerServiceWorker(); |
Further example
Let’s make another component where we take an array, loop over it, and display the data. Define the component.
We add the nameArray property. Then we use the map function to look over each element in nameArray. We use parameter name and index to display the array data.
1 2 3 4 5 6 7 8 9 10 |
class ListWorld extends React.Component { render() { return (<ul> {this.props.nameArray.map(function(name, index){ return <li key={index}>{index}, {name}</li> })} </ul>) } } |
and we call the component like so:
1 |
<ListWorld nameArray= {["Ricky", "Vera", "John"]} /> |
output at localhost:3000:
Hello, Batman !!
0, Ricky
1, Vera
2, John
Hello, Superman !!
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; import registerServiceWorker from './registerServiceWorker'; class HelloWorld extends React.Component { render() { return <p>Hello, {this.props.greetings} !! </p> } } class ListWorld extends React.Component { render() { return (<ul> {this.props.nameArray.map(function(name, index){ return <li key={index}>{index}, {name}</li> })} </ul>) } } <pre> ReactDOM.render( <div id="row"> <HelloWorld greetings="Batman" /> <ListWorld nameArray= {["Ricky", "Vera", "John"]} /> <HelloWorld greetings="Superman" /> </div>, document.getElementById('root')); registerServiceWorker(); |