In this session, you performed the following tasks:
- Environment Setup
You installed Node.js and create-react-app node package. Using this package, you created your React application viz. Phone Directory. You started your development server on your local machine to run this application.
You also looked at what all files and folders are automatically created by the create-react-app package and what are they meant for.
- Code Cleanup
You deleted the unnecessary code from the auto-generated application folder.
After performing the aforementioned tasks, you learned the following concepts in React:
- JSX
Though JSX is HTML-looking syntax, it is actually XML extension to ECMAScript specification. Thus, instead of using pure JavaScript for building DOM elements, you can use JSX, which offers flexibility to developers to use a familiar syntax, viz. HTML.
- Differences between JSX and HTML
You looked at how the following points hold true for React but not for HTML:
- Adjacent JSX elements must be wrapped in an enclosing tag
- Closing tag required for all types of tags
- JSX properties are not similar to HTML attributes
- Case sensitiveness
- Curly Braces
These can be used in React to evaluate a JavaScript expression during compilation. The expression can be a variable, a function, an object, an arithmetic calculation, logical evaluation, or any code snippet that returns some value.
- React.createElement() method
This method is used when an element needs to be created on DOM.React.createElement(element_name, element_properties, children);
The first argument in this method is the name of the element that is to be rendered. The second argument is the object that consists of property-value pairs that can be provided as attributes to this component or element. After these two arguments, you can pass an infinite number of children elements, which will be nested inside this main component or element.The first argument is mandatory, while the rest of the arguments that follow are optional.
- ReactDOM.render() method
This method is used to render an element into DOM in React. ReactDOM.render(argument_1, argument_2);
argument_1 tells WHAT to render.
argument_2 tells WHERE to render.
- Components
Components are just the JavaScript way of writing independent, reusable, and dynamic code. There are two types of components in React — functional components, which are written in the form of functions and class components, which are written in the form of classes.
- Styling components
React offers styling in two ways — inline and external.
Inline Styling:
- Style property is used.
- Two curly braces are used with the style property — one to evaluate the expression inside the JSX code and the other to represent a JavaScript object, which is taken as input by the style property.
- The property names must be written in camelCase such as textTransform, fontSize, etc.
- All the property values must map to a valid datatype such as ‘#000’ as the color code should be written within single/double quotes to make it a string value.
- All property-value pairs are separated using the comma operator.
External Styling: - You need to create an external stylesheet and define all the CSS styles inside this stylesheet. Then, you need to import this stylesheet inside the file where that component or element is defined on which this style is to be applied.
- Dynamically injecting data using map() method
JavaScript’s map() method can be used to iterate over an array and inject data into the React components or elements dynamically. You don’t need to hard-code the data inside each component. This is one of the major reasons why React refers to its components as ‘reusable’ entities.
You have also built some portion of the Phone Directory application.