ReactJS Introduction, File Structure, CSS Implementation

Introduction

React is a declarative, efficient, and flexible JavaScript library for building user interfaces. React makes it painless to create interactive UIs. React is a Component based App. Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.

React can also render on the server using Node and power mobile apps using React Native.

Installation

You’ll need to have Node >= 8.10 and npm >= 5.6 on your machine. You can install the react from this url https://reactjs.org/docs/create-a-new-react-app.html

Create Project

You can create the project using npx in any platform using following command.


npx create-react-app project-name
cd project-name
npm start

Hello World Example

Try this simple code example. Copy below code in app.js

import React from 'react';
function App() {
   return (
       <div> Hello World </div>
   );
}

File Structure

In React application, there are several files and folders in the root directory. Some of them are as follows:

node_modules: It contains all React dependencies or call libraries and script required for the project.

public: It holds the public assets of the application. It contains the index.html where React will mount the application by default on the < div id="root" >< /div > element.
public/index.html : It is the only html file in your project. The component going to change dynamically but this html file responsible to serve the all view.

src: It contains the App.css, App.js, App.test.js, index.css, index.js, and serviceWorker.js files. Here, the App.js file always responsible for displaying the output screen in React.

src/index.css: Apply style to body tag and logo
src/index.js: Specify the Root component which situated in index.html
src/app.css: css apply to app component
src/app.js: App component represent the view that display in index.html

package.json: It holds various metadata required for the project. It gives information to npm, which allows to identify the project as well as handle the projects dependencies.

README.md: It provides the documentation to read about React topics.

Styling and CSS

1. CSS Stylesheet

CSS classes are generally better for performance than inline styles. To add the css class, pass a string as the className property.

Create myStylesheet.css
.error { color : red }

To use above css, you must import this css file to code file, in our case app.js


import React from 'react';
import ‘./myStylesheet.css’

function App() {
  return (
    <div className=”error”>Error Div</div>
   );
}
export default App;

2. Inline Styling

The style attribute accepts a JavaScript object with camelCased properties rather than a CSS string. This is consistent with the DOM style JavaScript property, is more efficient, and prevents XSS security holes. For example:


import React from 'react';
function App() {
  const Error = {
    color : 'red'
  };
  return (
     <div style={Error}>Error Div</div>
  );
}
export default App;

3. CSS Modules Stylesheet

This project supports CSS Modules alongside regular style-sheets using the [name].module.css file naming convention. CSS Modules let you use the same CSS class name in different files without worrying about naming clashes.

Create one file appStyle.module.css
.success { color : ‘green’ }

Then in app.js file used the below code to access this success class


import React from 'react';
import styles from './appStyle.module.css';

function App() {
  return (
  <div className={styles.success}>Success Div</div>
  );
}
export default App;

You can also add the bootstrap classes inside the script, but you must install the bootstrap first in react.