Reactjs Notes
Reactjs Notes
What is React?
1. Read-Only: Props are immutable; they cannot be modified by the receiving component.
2. Parent-to-Child Communication: Props allow data to flow from parent to child components.
3. Reusable Components: By passing different props, you can reuse components with varying data or behavior.
1. Passing Props: You pass props to a component by adding attributes to the component tag.
2. Accessing Props: In the child component, you access props through the props object.
4. Event Pooling: Synthetic events are pooled and reused to improve performance, meaning the event is nullified after the handler executes.
Example
jsx
Copy code
function BookStore() { function handleTitleChange(e) { console.log("The new title is:", e.target.value); // Synthetic event console.log
Output
Why Use Synthetic Events?They abstract browser differences and make event handling predictable in React applications.
Inline conditional expressions allow you to conditionally render content directly within JSX, without requiring separate functions or statements. React supports JavaScript
expressions, enabling conditions to be embedded in JSX using syntax like ternary operators or logical operators (&&, ||).
Below are the list of reasons to prefer fragments over container DOM elements,
1. Fragments are a bit faster and use less memory by not creating an extra DOM node. This only has a real benefit on very large and deep trees.
2. Some CSS mechanisms like Flexbox and CSS Grid have a special parent-child relationships, and adding divs in the middle makes it hard to keep the desired layout.
3. The DOM Inspector is less cluttered.
Apart from the advantages, there are few limitations of React too,
1. React is just a view library, not a full framework.
2. There is a learning curve for beginners who are new to web development.
3. Integrating React into a traditional MVC framework requires some additional configuration.
4. The code complexity increases with inline templating and JSX.
5. Too many smaller components leading to over engineering or boilerplate.
React renders HTML to the web page by using a function called render(). The purpose of the function is to display the specified HTML code inside the specified HTML
element. In the render() method, we can read props and state and return our JSX code to the root component of our app.
1. Router(usually imported as BrowserRouter): It is the parent component that is used to store all of the other components. Everything within this will be part of
the routing functionality
2. Switch: The switch component is used to render only the first route that matches the location rather than rendering all matching routes.
3. Route: This component checks the current URL and displays the component associated with that exact path. All routes are placed within the switch components.
4. Link: The Link component is used to create links to different routes.
Initialization: This is the stage where the component is constructed with the given Props and default state. This is done in the constructor of a Component Class.
Mounting: Mounting is the stage of rendering the JSX returned by the render method itself.
Updating: Updating is the stage when the state of a component is updated and the application is repainted.
Unmounting: As the name suggests Unmounting is the final step of the component lifecycle where the component is removed from the page.
Prop drilling is basically a situation when the same data is being sent at almost every level due to requirements in the final level. The problem with Prop Drilling is that
whenever data from the Parent component will be needed, it would have to come from each level, Regardless of the fact that it is not needed there and simply needed in
last.