top of page
Logo der Online Agentur mdwp

Modules

In React Js, Modules are portions of code that are logically grouped together to perform a particular function. Modules could include functions, classes or components. A key idea behind modules is the principle of separating concerns, meaning each module performs a distinct function independent of others. This approach promotes reuse and easier maintenance of the code.

You can create a module in a separate file and then export it to be imported and used in another file.

Suppose we have a function named `greet` in a file named `greet.js`:

```javascript
// greet.js

function greet(name) {
return `Hello, ${name}`;
}

export default greet;
```

In the code above `greet` is a module that we're exporting to be used in other parts of the application. We can import it into another file like so:

```javascript
// app.js

import greet from './greet';

console.log(greet("John")); // Outputs: "Hello, John"
```

In the second file `app.js`, we import the `greet` function from the `greet.js` file and use it.

So in React Js, Modules allow us to write clean, reusable and maintainable codes.

bottom of page