To get started with building a project follow the steps on this link.
Now every time you are opening your project you just need:
npm start
Your project will be running live in your browser. Any changes made will be reflected there. If everything went smoothly and it is your first run, you will see something similar to this
As we know that our react app is made up of components. In our directory, we can see App.js. This is going to be our root component. All other components will be nested in this only.
In index.js we can see ReactDOM.render()
. It is used to render our components. We are passing App as our 'root' component. Note that here we can even write HTML/JSX in place of App />
. It will work just fine, but that defeats the purpose of using react as we want to work with components only.
Inside App.js, one way of making a component is shown. As we can see, we need to import from 'react'
library. This component needs to have a render()
function defined. Also, we can see HTML like syntax in between. It looks like it but behind the scene is converted to js only. There are certain differences from normal HTML like the className
here is different.
The HTML we saw above is basically JSX. Below code has similar effect. But this time we don't use JSX.
As we can see instead of the JSX(commented) we use React.createElement()
(Reason of importing React). It can have many parameters, but here we have three.
h1
element again using React.createElement()
.{className}
(configuration)
So this is how JSX gets used behind the scene. For more see.
Note that when we are using JSX, as best practice we will only create a root element. And no other adjacent elements.
Now we will try to create another component. For this, I created a folder named Food
in the same directory and inside it Food.js
. This is how the naming convention is i.e. with the first letter capitalised. Inside food.js:
Using arrow notation to create a component. It returns some JSX (for which we need React import). Note that we can create components using class (like in App.js) or using function(here).
Now importing in App.js
First, we imported the Food.js
. And then adding it to our JSX with <Food/>
. Note that the first letter of component is kept capital to tell react that it is a 'custom component'.
Result:
Now the advantage of making a component are:
App.js
doesn't get crowded; especially for a bigger applicationWe can provide some dynamic output from within our JSX. We write some JS in curly braces for this. Example: In Food.js
const food = () => {
return <p> Some nutritional information! of food num {Math.floor(Math.random()*30)}</p>
}
Here, we are generating random numbers using Math.random(). Similarly, we can make a function call or give one-line expressions. We can even give property values from App.js and then use in our component template. For example:
//in App.js
<Food name = "Orange" vitamin = "C"/>
Here we defined attributes name and vitamin and assigned value to them. These can be accesses in template(Food.js) using object named props. We can name it anything we like but that is the convention. We pass props as parameter to our food function and then we access it in our JSX.
//in Food.js
const food = (props) => {
return <p> Some nutritional information! of food: {props.name} containing vitamin {props.vitamin}</p>
}
Our App.js now looks like:
Output: