Important terms when starting with React.

We already created our first react app using create-react-app cli. Now we’ll learn some of the core terminologies involved in react.

1.Component :- its the building block of react app, just like functions, we create components to perform some functionality. It takes the given parameters (called as props) and render UI in the browser by consuming the given parameters.

Example:- we need to display contact details of a person so we’ll create the below component which takes the props as parameter when we use this component.

a)Using class syntax to create component, 

1
2
3
4
5
6
7
Class componentName extends Component {
render(){
return(
<our plain html + props passed to component>
)}
}
export default componentname

b)Component uses the render method to return the UI component to the calling parent component.

c)Export the component and it will imported with this name and used inside calling parent component
d)Calling a component with props.

2.Props, its like parameters to components like we have arguments to a function, we use them to pass data to components.

a)How to pass data :-
b)Access the prop in the component by using this keyword (refering to the current context)

{this.props.contact.name}

c)Anything which is javascript expression in jsx is presented inside {} (curly brackets)

Note:- props refer to attributes from parent components. It represents “read-only” data that are immutable.

3.Stateless function component, incase our component just returns the UI component we can use function to create a component instead of creating a class

a)What changes we have to do? 

  • Function will take passing prop as argument
  • Returns the description UI
  • this keyword is not used in accessing the props inside function.
b)Example, we replaced the our class component with “stateless function component”
Class component:-

 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
class ContactList extends Component{

render(){
console.log(this.props);
return(
<ol className="contact-list">
{
this.props.contact.map(contact =>
<li key={contact.id} className="contact-list-item">
<div className="contact-avatar" style={{
backgroundImage:`url(${contact.avatarURL})`
}}/>
<div className="contact-details">
<p>{contact.name}</p>
<p>{contact.email}</p>
</div>
<button className="contact-remove">
Remove
</button>
</li>
)
}

</ol>
)
}

}

export default ContactList;


Stateless function component for above class component:-

 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
import React, { Component } from 'react'


function ContactList(props){
return(
<ol className="contact-list">
{
props.contact.map(contact =>
<li key={contact.id} className="contact-list-item">
<div className="contact-avatar" style={{
backgroundImage:`url(${contact.avatarURL})`
}}/>
<div className="contact-details">
<p>{contact.name}</p>
<p>{contact.email}</p>
</div>
<button className="contact-remove">
Remove
</button>
</li>
)
}

</ol>
)
}

export default ContactList;

4.State:-  represents mutable data that ultimately affects what is rendered on the UI. State is managed internally by the component itself and is meant to change over time, commonly due to user input (e.g., clicking on a button on the page).

a)To create state it should be declared inside a Class

1
2
3
4
5
6
7
8
state = {
contacts : [
{
"id": "test",
"name": "Rion flower",
}
]
}


b)How to pass state to a component

1
2
3
4
5
6
7
render() {
return (
<div>
<ContactList contact ={ this.state.contacts }/>
</div>
)
}


One of the key benefits of using State in React to build UI components: when it comes to re-rendering the page, we just have to think about updating state. We don’t have to keep track of exactly which parts of the page change each time there are updates. We don’t need to decide how we will efficiently re-render the page. React compares the previous output and new output, determines what has changed, and makes these decisions for us. This process of determining what has changed in the previous and new outputs is called Reconciliation.

Note:- In React UI is function of state, when state changes UI also changes.
More Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *