React Js
React Js
components.
Useful for handling the view layer for web and mobile apps.
React is an open-source, component-based front end library which is responsible only for the
view layer of the application.
React can also render on the server using Node, and it can power native apps using React
Native.
Components − React is all about components. You need to think of everything as a component.
This will help you maintain the code when working on larger scale projects.
Unidirectional data flow and Flux − React implements one-way data flow which makes it easy to
reason about your app. Flux is a pattern that helps keeping your data unidirectional. ( Action
Store View )
Virtual DOM
Simplicity
Performance
To Install NodeJS
https://nodejs.org/en/
Webpack is a module bundler (manages and loads independent modules). It takes dependent modules
and compiles them to a single (file) bundle. You can use this bundle while developing apps using
command line or, by configuring it using webpack.config file.
Babel is a JavaScript compiler and transpiler. It is used to convert one source code to other. Using this
you will be able to use the new ES6 features in your code where, babel converts it into plain old ES5
which can be run on all browsers.
1. Install create-react-app
npm start
node_modules: It contains the React library and any other third party libraries needed.
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.
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.
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 project?s dependencies.
React uses JSX for templating instead of regular JavaScript. It is not necessary to use it.
It is also type-safe and most of the errors can be caught during compilation.
It makes it easier and faster to write templates, if you are familiar with HTML.
render() {
return (
<div>
<h1>Header</h1>
</div>
);
}
export default App;
Ex:
render() {
Setup:
npm start
render() {
return (
<div className="App">
<h1>Hello World!</h1>
</div>
);
}}
index.js
ReactDOM.render(myfirstelement, document.getElementById('root'));
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport"
<title>React App</title>
</head> <body>
<div id="root"></div>
</body>
</html>
The ReactDOM.render() function takes two arguments, HTML code and an HTML element.
The purpose of the function is to display the specified HTML code inside the specified HTML element.
Example
ReactDOM.render(<p>Hello</p>, document.getElementById('root'));
<body>
<div id="root"></div>
</body>
What is JSX?
JSX stands for JavaScript XML.
JSX allows us to write HTML elements in JavaScript and place them in the DOM without
any createElement() and/or appendChild() methods.
ReactDOM.render(myelement, document.getElementById('root'));
The expression can be a React variable, or property, or any other valid JavaScript expression.
const myelement = (
<div>
<h1>I am a Header.</h1>
<h1>I am a Header too.</h1>
</div>
);
JSX follows XML rules, and therefore HTML elements must be properly closed.
Example
Close empty elements with />
React Components
Components are independent and reusable bits of code. They serve the same purpose as
JavaScript functions, but work in isolation and returns HTML via a render function.
Components come in two types, Class components and Function components, in this tutorial we
will concentrate on Class components.
The component has to include the extends React.Component statement, this statement creates
an inheritance to React.Component, and gives your component access to React.Component's
functions.
The component also requires a render() method, this method returns HTML.
Example
Create a Class component called Car
Props are like function arguments, and you send them into the component as attributes.
Example:
constructor(props) {
super(props);
render() {
return (
<div>
<h1>My Car</h1>
</div>
);
When a value in the state object changes, the component will re-render, meaning that the
output will change according to the new value(s).
Props State
Props allow you to pass data from one component to other components State holds information about the components.
as an argument.
Props can be accessed by the child component. State cannot be accessed by child components.
Props are used to communicate between components. States can be used for rendering dynamic changes with the comp
Stateless component can have Props. Stateless components cannot have State.
Props are external and controlled by whatever renders the component. The State is internal and controlled by the React Component itse
Lifecycle of Components
Each component in React has a lifecycle which you can monitor and manipulate during its three
main phases.
Initial Phase
getDefaultProps()
It is used to specify the default value of this.props. It is invoked before the
creation of the component or any props from the parent is passed into it.
getInitialState()
It is used to specify the default value of this.state. It is invoked before the
creation of the component.
Mounting Phase
componentWillMount()
This is invoked immediately before a component gets rendered into the DOM. In
the case, when you call setState() inside this method, the component will
not re-render.
componentDidMount()
This is invoked immediately after a component gets rendered and placed on the
DOM. Now, you can do any DOM querying operations.
render()
This method is defined in each and every component. It is responsible for
returning a single root HTML node element. If you don't want to render anything,
you can return a null or false value.
Updating Phase
componentWillRecieveProps()
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()
Unmounting Phase
componentWillUnmount()
Forms
Uncontrolled component
Controlled component
In HTML, form elements typically maintain their own state and update it according
to the user input. Here, the mutable state is kept in the state property and will be updated only
with setState() method
Hooks are the new feature introduced in the React 16.8 version. It allows you to use state and other
React features without writing a class. Hooks are the functions which "hook into" React state and
lifecycle features from function components. It does not work inside classes.
Rules of Hooks:
Hooks are similar to JavaScript functions, but you need to follow these two rules when using them.
Hooks rule ensures that all the stateful logic in a component is visible in its source code. These rules are:
Basic Hooks
useState
useEffect
useContext
Additional Hooks
useReducer
useCallback
useMemo
useRef
useImperativeHandle
useLayoutEffect
useDebugValue
function CustomCounter() {
// useEffect(() => {
// });
React Redux:
Redux is an open-source JavaScript library used to manage application state. React uses Redux for
building the user interface. It was first introduced by Dan Abramov and Andrew Clark in 2015.
It allows React components to read data from a Redux Store, and dispatch Actions to
the Store to update data.
STORE: A Store is a place where the entire state of your application lists. It manages the status
of the application and has a dispatch(action) function. It is like a brain responsible for all moving
parts in Redux.
ACTION: Action is sent or dispatched from the view which are payloads that can be read by
Reducers. It is a pure object created to store the information of the user's event. It includes
information such as type of action, time of occurrence, location of occurrence, its coordinates,
and which state it aims to change.
REDUCER: Reducer read the payloads from the actions and then updates the store via the state
accordingly. It is a pure function to return a new state from the initial state.
Create Components
The App component will be used as a tab menu. The other three components (Home),
(About) and (Contact) are rendered once the route has changed.
Add a Router
we will add routes to the app. Instead of rendering App element like in the previous example, this time
the Router will be rendered. We will also set components for each route.
Higher order components are JavaScript functions used for adding additional functionalities to
the existing component.
These functions are pure, which means they are receiving data and returning values according
to that data
Higher Order Component (HOC) is wrapping around "normal" component and provide additional data
input. It is actually a function that takes one component and returns another component that wraps the
original one