React JS
Published: 2024-02-08 15:27:49

There are two primary ways to export values with JavaScript: default exports and named exports. So far, our examples have only used default exports. But you can use one or both of them in the same file. A file can have no more than one default export, but it can have as many named exports as you like.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvhnq6rxpdcdiuv29qdj.png)

Default and named exports
How you export your component dictates how you must import it. You will get an error if you try to import a default export the same way you would a named export! This chart can help you keep track:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fd91k767l86za3o451t.png)

When you write a default import, you can put any name you want after import. For example, you could write import Banana from './Button.js' instead and it would still provide you with the same default export. In contrast, with named imports, the name has to match on both sides. That’s why they are called named imports!

People often use default exports if the file exports only one component, and use named exports if it exports multiple components and values. Regardless of which coding style you prefer, always give meaningful names to your component functions and the files that contain them. Components without names, like export default () => {}, are discouraged because they make debugging harder.

**_<u>Pure Components</u>_**

**Note**
React there are three kinds of inputs that you can read while rendering: props, state, and context. You should always treat these inputs as read-only.

When you want to change something in response to user input, you should set state instead of writing to a variable. You should never change preexisting variables or objects while your component is rendering.

**Note**
Strict Mode has no effect in production, so it won’t slow down the app for your users. To opt into Strict Mode, you can wrap your root component into <React.StrictMode>. Some frameworks do this by default.

However, it’s completely fine to change variables and objects that you’ve just created while rendering. In this example, you create an [] array, assign it to a cups variable, and then push a dozen cups into it:

```
function Cup({ guest }) {
  return <h2>Tea cup for guest #{guest}</h2>;
}

export default function TeaGathering() {
  let cups = [];
  for (let i = 1; i <= 12; i++) {
    cups.push(<Cup key={i} guest={i} />);
  }
  return cups;
}
```
If the cups variable or the [] array were created outside the TeaGathering function, this would be a huge problem! You would be changing a preexisting object by pushing items into that array.

However, it’s fine because you’ve created them during the same render, inside TeaGathering. No code outside of TeaGathering will ever know that this happened. This is called “local mutation”—it’s like your component’s little secret.

Functional V/S Class Components

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8adps9mazuwslmbwt91p.png)

ES6 Update ->

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqqeusxja6gl7v8jl2g8.png)

**JSX tag**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tf9cm730q0k0fch7s2e0.png)

**React Library -> CreateElement method**

**Create dom in dom element in React**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke7ynmmrlhw0j8q1bq33.png)

JSX transfroms to React.createElement() which in turn uses React library and so we import React library everywhere


In the above case JSX keeps the code elegant if there more no. of components 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3ksbsnzeq1waz1ww2zf.png)


**Class Components**
![Uploading image](...
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22o2o731n1vyol7z9ac2.png)

**Using this keyword**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ts0meou5duu8kowojkba.png)

**React Props**
Can not be written can only be read.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhkgjgku6q0fzopyej74.png)

**setState**
They are asynchronous operations.
sets the state such that we can change the state of an object or variable within the component but these are defined inside the component only.