Pallaveechaubey / React

0 stars 0 forks source link

react-document

Table of Content

What is React?

Advantage of React

What is Tailwind CSS?

Why we are integrating tailwindcss with react project?

What is Create React app

JSX

Why JSX?

What are HTML attributes?

Setup for React Project

Reference

What is React?

React is a front-end Javascript library. It is an open-source component-based library that is in charge of developing the application's view layer. Since its initial release in 2013, React has become one of the most popular javascript libraries for UI development. It is easier to reason about and debug, and its capacity to effectively alter the user interface in response to data changes.

Advantage of React

What is Tailwind CSS?

Tailwind CSS is like a toolbox for styling websites or apps. Instead of giving you pre-made styles for things like buttons or headers, it gives you lots of small tools (called utility classes) that you can use to style elements directly in your HTML code.

For example, instead of having a class for a big blue button, you might use utility classes to make a button big, blue, and rounded.

Why we are integrating tailwindcss with react project?

When you use a CSS framework like Bootstrap, it gives you a bunch of ready-made styles that you can apply to things like buttons or tables just by adding certain classes to your HTML code.

But with Tailwind CSS, it's a bit different. Instead of giving you pre-made styles, Tailwind CSS gives you small building blocks called utility classes. These classes let you control things like margins, padding, colors, and more directly in your HTML code.

What is Create React app

JSX

Jsx stands for javascript XML. It is a syntax extension for javascript that allows you to write HTML-like elements in our javascript code. it is used in react a describe the structure and content of a component.

One of the main benefits of JSX is that it makes it easy to create and manipulating the structure of a components.

const name =<h1> hello Bachoo</h1>

A JSX elements is converted into a regular javascript object during compilation so that it can be used to construct genuine DOM elements. For instance, the javascript generated from the JSX code above might be as follows:

const name = React.createElement("hi", null,"hello bachoo");

Basic difference between JSX and HTML

Browser can't react Jsx

JSX is a combination of HTML and JavaScript. so, it is not supported by the browser. so a transpiler called babel converts the JSX into pure javascript. Then browser understands the code and executes it. Browsers can't read JSX because there is no inherent implementation for the browser engines to react to and understand them.

Why JSX?

With the help of JSX, we can write HTML code with javascript. React does not employ the createElement() method; Instead, JSX elements are used to create HTML elements. As a result, JSX facilitates the writing and addition of HTML components in React. A transpiler called babel.js will convert Jsx to javascript on the browser.

Advantages of Jsx

Note

Ternary operation inside Jsx elements

The ternary operator can be used with JSX components to conditionally render different elements or components based on certain conditions. The ternary operator is shorthand version of if/else statement and has the following syntax :

Condition? true:false

Here is an example of ternary operators within a JSX component:

functionMyComponents(props){
    return(
        <div>
        {props.isLoggedIn ? <p>Welcome back!</p> : <p>please log in</P>}
    )
}

In this example the components receive props called isloggedin and use the ternary operator to determine whether to show a "welcome back message " or please login message. The condition is the prop is logged in is true it will render the first element and if it is false it will render the second element .

It is worth noting that the ternary operator is more concise than an if/else statement but it can be less readable when the conditions are complex in that case is recommended to use if/else statement.

What are HTML attributes?

HTML attributes are properties that are used to define the characteristics and behavior of HTML elements. they are added to the opening tag of HTML element and come in the form of name-value pairs, with the name being the attribute and the value being what it set to

For example, commonly used attributes include "class", "id", "src", "href", "for" etc, and events like "onclick".

<div class="my-class">Hellow world </div>

But in react we use 'className' attribute instead of class.

Let's take another example of on click event in HTML. In HTML we use "on click in all lower case but in react we use camel case like 'onClick " " Also in HTML, we have to put the function in quotes and invoke it by putting parameters but in React we use curly braces and the function name without parenthesis.

NOTE--

We already know there are certain differences between html and jsx and one such difference that we have seen in html. In normal HTML we have for but we cannot use it in javascript since it is a reserved keyword in javascript so we use htmlFor in jsx.

Introducing the new root API

A root in react refers to the top-level data structure that renders a tree. In React 18, we will have two root APIs: Legacy root API and New root API

Legacy root API

The legacy root API is the existing API called the reactDOM.render method. It will create a root running in legacy mode, which is similar to usage in each version 17. If we are using reactDOM.render() in your react 18 app, we will definitely see the below issue.

AJSX element is converted into a regular javascript object during compilation so that it can be used to construct a genuine DOM element.

The reason in ReactDOM.render is no longer supported in React 18. Earlier, we used to render the component in the below way:

ReactDOM.render(<NavBar/>, document.getElementById('root'))

The render() method of the react-dom package is considered legacy starting react-dom version 18. The method is replaced with createRoot() method that is exported from react-dom/client. The createRoot() method taked the root elements as parameters and creates a react root.

New Root API

createRoot() is a new method introduced in React 18 that allows you to create a separate root for a react tree outside of the main react DOM tree. it take a single argument, which is a reference to an existing DOM element. It returns a new root object that has a render() method for rendering react components into the specified DOM element.

call createRoot to create a React root for displaying the content inside the browser DOM element.

// Import ReactDOM from 'react-dom';
import ReactDOM from 'react-dom';

// Import the App component (assuming it's defined in a separate file)
import App from './App';

// Get the root DOM element where the React app will be rendered
const rootNode = document.getElementById('root');

// Old way of rendering React component
// ReactDOM.render(<App />, rootNode);

// New way of rendering React component using Concurrent Mode and createRoot
import ReactDOM from 'react-dom/client';

// Create a root instance using createRoot, passing the root DOM element
const root = ReactDOM.createRoot(rootNode);

// Render the App component inside the root
root.render(<App />);

Explanation:

Old way (ReactDOM.render()):

Using Concurrent Mode and createRoot() is the recommended approach for rendering React components in newer applications, especially when dealing with large or complex UIs, as it can provide better performance and user experience.

Setup for React Project

Install node.js

https://nodejs.org/en

Check if the node is successfully install or not using the below command

node -v
npm -v

select the framework

cd first_project
npm install
npm run dev

Integrate tailwindcss with react project

npm install -D tailwindcss
npx tailwindcss init
content: ["./src/**/*.]

Reference

https://legacy.reactjs.org/docs/getting-started.html https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Client-side_JavaScript_frameworks/React_getting_started https://www.w3schools.com/REACT/react_intro.asp