Interview Questions
Interview Questions
Questions on ReactJS********
======================================================
======================================================
-----------------------------------------------------------------------------------
--
1) What is React ??
--------------------
---------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
3) What is JSX ??
--------------------
- variable declaration:
4) what is React.createClass ??
==============================
The end result is the same -- we have a component class. But the style is
different.
And one is using a custom JavaScript class system (createClass) while the other is
using a "native" JavaScript class system.
render(){
return (
<div> .....</div>
);
}
});
Instead of using a method from the react library, we extend an ES6 class that the
library defines, Component.
constructor(props){
super(props);
}
render(){
return(
<div> ----- </div>
);
}
}
- This package serves as the entry point to the DOM and server renderers for
React.
- It is intended to be paired with the generic React package, which is shipped as
react to npm.
Installation
=============
npm install react react-dom
In the browser
==============
On the server
============
ReactDOMServer.renderToString(<MyComponent />);
API
====
react-dom
findDOMNode
render
unmountComponentAtNode
react-dom/server
renderToString
renderToStaticMarkup
--------------------------------------------------------------------------------
REACT
=====
ANGULAR
=======
-------------------------------------------------------------
reactjs
=======
angularjs
=========
Vuejs
=====
1.Vue.js is the progressive framework for java script which builds user interfaces
and single page applications.
2.Vue js is very easy to set-up and execute faster compare to other framework.
Nodejs
=====
1.Node.js is an open source runtime environment for developing server side and
networking application.
2.Node.js uses an event-based approach to address implicit scalability.
----------------------------------------------------------------------------------
Framework
=========
1.A framework is a skeleton where application defines its own features to fill out
the skeleton.
2.But in the frame work the control is vice versa of library where framework calls
or control.
Library
======
-----------------------------------------------------------------------------------
-----
No, React won’t follow MVC because it follows unidirectional data flow where
ever MVC follows two-directional data flow.
-----------------------------------------------------------------------------------
----
1>Class component.
2>Function component.
-------------------------------------------------------------------------
constuructor(){
super();
this.state={ Name:”Sai” }
}
render (){
return(
<div>{ this.state.Name}</div>
)}
}
-----------------------------------------------------------------------------------
----
- React props are like function arguments in javaScript and attributes in HTML.
- React allows us to pass information to a component using something called props.
- To pass the data form parent component to child component we can use props.
- Props are immutable(can’t change the value of props).
Ex:
===
-----------------------------------------------------------------------
state
--------
function Example() {
const [count, setCount] = useState(0);
}
props
-----
--------------------------------------------------------------------------
-----------------------------------------------------------------------------
i) . CSS Stylesheet
-------------------
Simply import css file import './DottedBox.css' so you can have a separate css file
for each component.
iv) Styled-components
----------------------
Styled-components is a library for React and React Native that allows you to use
component-level styles in your application that are written with a mixture of
JavaScript and CSS
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------
Every component has a separate html file. However, in React, I see that render
function itself includes the html template.
outside the js file and put it in a separate html and import the html file to
render function, for example
render() {
return (
import content of helloworld.html
);
}
-----------------------------------------------------------------------------------
-----------------------------------------------------
At the moment I'm using three components: <list />, < Filters /> and <TopBar />,
now obviously when I change settings in < Filters />
I want to trigger some method in <list /> to update my view.
Scenario - 1
============
You could pass a handler from <List /> to <Filters />, which could then be called
on the onChange event to filter the list with the current value.
Scenario 2
------------
Similar to scenario #1, but the parent component will be the one passing down the
handler function to <Filters />, and will pass the filtered list to <List />. I
like this method better since it decouples the <List /> from the <Filters />.
=============================================================================
-----------------------------------------------------------------------------------
------------------------------------------------------------
----------------------------------------------------------------------------------
- The reducer is a pure function that takes the previous state and an action, and
returns the next state.
- newState =(initialState,action)
- It "reduce" a collection of actions and an initial state (of the store) on which
to perform these actions to get the resulting final state.
-----------------------------------------------------------------------------------
-------
-----------------------------------------------------------------------------------
---------------
The state can be changed by the component itself by calling set state which will
trigger a re-render of the component. The state is managed in a container that also
includes methods to work with a state object. A container looks and feels like any
React component without the UI-part. Also, the setState function follows React’s
setState the only difference is, that unstated setState returns a Promise you can
await.
-----------------------------------------------------------------------------------
------------------
-----------------------------------------------------------------------------------
----------------------------
-----------------------------------------------------------------------------------
-------------------------------
Redux saga :
============
- Sagas created by using “generator” functions.
- Sagas takes advantage of the “yield” keyword to halt execution with in a
function.
- Easy to test
- It is useful to express complex application logic.
Redux thunk:
===========
- Thunk created by using “promises” funtions.
- Promises simple to use.
- Thunk is a function that already has everything it needs to execute.
- Difficult to test.
- It is good for small use cases and for beginners.
- Thunk logic is all contained inside the function.
-------------------------------------------------------------------------
32) (how to provide the communication between reducers ??)
How to Combine reducers?
===========================================================
-----------------------------------------------------------------------------------
--------
Ex: If your app grows more complex, if you want to split your reducing function
into separate functions
- The combineReducers helper functions turns an object whose values are different
reducing functions into a single reducing function you can pass to createStore.
-----------------------------------------------------------------------------------
------------------------
ReactDOM.render(<Provider store =
{store}><App/></Provider>,document.getElementById(‘root’));
-----------------------------------------------------------------------------------
----------------
- "HOCs" is the Redux framework which is generally used for the state
management in complex ReactJS projects.
-----------------------------------------------------------------------------------
-----------------------------------------
(1) GETDEFAULTPROPS():
--------------------
Syntax:
---------
getDefaultProps:function(){
return{ Hello };
};
(2) GETINITIALSTATE():
--------------------
Syntax:
----------
getInitialState:function(){
return{ Hello } ;
};
(3) COMPONENTWILLMOUNT():
-------------------------
componentWillMount:function(){
};
(4)RENDER():
-------------
this.setState({
counter:
})
syntax:
render:function(){
(5) COMPONENTDIDMOUNT():
------------------------
Syntax:
----------
componentDidMount:function(){
};
(6) COMPONENTWILLRECEIVEPROPS():
--------------------------------
Syntax:
---------
componentWillReceiveProps:function(newProps){
};
(7) SHOULDCOMPONENTUPDATE():
--------------------------
- This is always called before the render method and enables to define
if a re-rendering is needed or can be skipped based on the boolean value it
returns.
Syntax:
---------
shouldComponentUpdate:function(nextProps,nextState){
return true;
};
(8) COMPONENTWILLUPDATE():
--------------------------
Syntax:
---------
componentWillUpdate:function(nextProps,nextState){
};
(9) COMPONENTDIDUPDATE():
-------------------------
Syntax:
---------
componentDidUpdate:function(prevProps,prevState){
};
(10) COMPONENTWILLUNMOUNT():
-----------------------------
- called once. after the render method is called and before the
component is removed from the DOM.
syntax:
----------
componentWillUnMount:function(){
};
-----------------------------------------------------------------------------------
-----------------------------------------------------
- Render() function returns the same result each time it's invoked.
-----------------------------------------------------------------------------------
-----------------------------------------------------------
- Pure Components compare all the properties of the current state with the
next state,and current props with the next props.
- Shallow comparison means that you compare the immediate contents of the
objects instead of recursively comparing all the key/value pairs of the object.
- Pure Components are ideal for classes with minimal and immutable
properties.
- Class Component can be pure too as long as their props and state are
immutable.
(40) what are the differences between stateful and stateless components in
Reactjs....?
===================================================================================
======
Stateful Components:
-----------------------
- stateless Components are those components which dont have any state at
all.
-----------------------------------------------------------------------------------
--------------------------------------------------
- Axios is a promise-based HTTP client that works both in the browser and in a
node.js environment.
- Axios is a lightweight HTTP client based similar to a Fetch API.
- Axios is promise-based async/await library for the readable asynchronous code.
- We can easily integrate with React.js, and it is effortless to use in any
frontend framework.
-----------------------------------------------------------------------------------
--------------------------------------------
To install the react router you need to download the react-router-dom package by
running the following commands.
-----------------------------------------------------------------------------------
----------------
npm install react-router-dom
npm start //to run dev server
If you navigate to the public/index.html you will see a single html file which
looks like this.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-
fit=no">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
users.js
===========
contact.js
========
app.js
=====
Now our app has three components one is App and the other two are Users and
Contact.
Routing
=======
open the index.js file and import the three components (App,Users,Contact)
index.js
=======
index.js
========
Now if you enter manually localhost:3000/users you will see Users component is
rendered.
But still, Home component is also rendered in the screen this happens because of
our home path is ’/’ and users path is ‘/users’ slash is same in both paths so that
it renders both components to stop this behavior we need to use the exact prop.
index.js
After adding navigation you will see the routes are rendered on the screen. if you
click on the users you will see url is changing and Users component is rendered.
-----------------------------------------------------------------------------------
------------------------------------------
Route.match.param.variableName
(`https://api.github.com/users/${props.match.params.id}`)
=================================================================
MERN Means Combination of four Technology client side ,server side database and
UI
->MondoDB(database)
->Express(For Api devilopment)
->ReactJs (For UI)
->Nodejs(For Server side)
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------
We’ll use create-react-app to get up and running quickly with a simple React app.
Install the package from npm and create a new app:
Next, let’s add bootstrap so that we can style our form easily:
Import Bootstrap CSS and optionally Bootstrap theme CSS in the beginning of the
src/index.js file:
import ‘bootstrap/dist/css/bootstrap.css’;
import ‘bootstrap/dist/css/bootstrap-theme.css’;
Ok, now let’s build the core of our demo app. Let’s add a Form component.
In src/App.js, let’s replace the default intro text markup with a Form component
that we’re going to build. We also need to import it:
-----------------------------------------------------------------------------------
--------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------
48)How to Create React Application?
Ans.Create React Application Mainly 8 Steps are there.
-----------------------------------------------------------------------------------
--------------
49) What is NPX ?
============================
NPX means Node Package eXecute.
NPX is a NPM package runner . its helps to execute packages without installing
explicity. NPX makes it easy to install and mange dependencies hosted in NPM
registry and its simplifies the process and provides a better for executables .
-----------------------------------------------------------------------------------
-----------------
2. "Babel" is popular tool for using the newest features of the JavaScript.
<script
src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.1/babel.min.js">
</script>
9. Babel will automatically transpile any script tags with type "text/babel" or
"text/jsx".
10. Example:
<div id="output"></div>
<script type="text/babel">
const getMessage = ()=> "Hello World";
document.getElementById('output').innerHTML = getMessage();
</script>
===================================================================================
===================================================================================
======================
2. Typescript is used for making React apps more stable, readable and manageable.
or
4. But TypeScript, requires much more time to compile as compare with JavaScript.
===================================================================================
===================================================================================
======================
2. The super() in javascript is used to call the methods of the parent class.
1. Virtual DOM in ReactJS makes user experience better and developer’s work faster.
7. JSX is an optional syntax extension to JavaScript that makes writing our own
components much easier.
2. ReactJS Covers only the UI Layers of the app and nothing else.
===================================================================================
===================================================================================
======================
-React uses plain JavaScript that makes React very simple to learn and
build a professional web pages.
3. React can be used to create mobile applications with the help of "React Native".
6. ReactJS uses Downward Data Flow. If we made any small changes in child structure
it won't affect the parents. Technically we call it "Code stability".
===================================================================================
===================================================================================
======================
4. In simple words we can say that DOM is a programming API for documents.
7. With the help of DOM a programmer can create and bulid documents, navigate their
structure and add, modify or delete elements and content.
===================================================================================
===================================================================================
======================
Q 57) what are the differences between DOM & Virtual DOM ??
--------------------------------------------------------------
===================================================================================
===================================================================================
======================
2. We can hide the implementation details of web component by using "Shadow DOM".
3. Example:
HTML5 slider input, while the regular DOM recognizes this as a simple
<input/> tag, there is underlying HTML and CSS that make slide feature.
4. The sub-tree of DOM nodes is hidden from the main DOM to encapsulate the
implementation of HTML5 slider.
6. "Isolate scope" make the components reusable and permit to control the binding.
===================================================================================
===================================================================================
======================
2. Example:
<button onClick={activateLasers}>
Activate Lasers
</button>
===================================================================================
===================================================================================
======================
4. Example:
<input value = {this.state.data}
onChange = {this.updateState}
ref = "myInput">
</input>
5. If "ref" points to a "standard component" (DOM node, such as input, select, div
etc) then to retrieve the element; we just need to call "this.refs.ref".
===================================================================================
===================================================================================
======================
Q 61) how to pass the data between components with out props ??
---------------------------------------------------------------
No we can not send data between two components without using props.
===================================================================================
===================================================================================
======================
4. Example:
Suppose we save a property called firstName and lastName in our state. We
want to display the full name in our component.
This "selector" will just join firstName and lastName and return the full
name.
===================================================================================
===================================================================================
======================
let i = 10;
let j = 20;
return function(){
console.log(i);
console.log(j);
};
add(...items) {
this.items.push(...items);
},
get(index) {
return this.items[index];
}
};
collection.get(1); //'Java'
4. Arrow function: An arrow function is defined using parenthesis that contains the
list of parameters followed by arrow symbol(=>).
- When arrow function has only "one parameter" parenthesis can be omitted.
- When it contains a single statement, the curly{} braces can be omitted too.
if (number < 0) {
return -number;
}
return number;
};
absValue(-10); //10
absValue(5); //5
a.function* <name>():
var index = 0;
while(true) {
yield index++;
}
}
const g = indexGenerator();
console.log(g.next().value); //0
console.log(g.next().value); //1
b. function* ():
let index = 0;
while(true) {
yield index++;
}
};
const g = indexGenerator();
console.log(g.next().value); //0
console.log(g.next().value); //1
c. *<name>():
*indexGenerator() {
var index = 0;
while(true) {
yield index++;
}
}
}
const g = obj.indexGenerator();
console.log(g.next().value); //0
console.log(g.next().value); //1
===================================================================================
===================================================================================
======================
2. If function has only one statement and the statement returns a value, we can
remove the brackets and the return keyword.
===================================================================================
===================================================================================
======================
7. Example:
export function Movie({ title, releaseDate }) {
return(
<div>
<div> Movie Title:{title} </div>
<div> Release Date:{releaseDate} </div>
</div>);
}
export const MemoizedMovie = React.memo(Movie);
===================================================================================
===================================================================================
=======================================================
=> State
=> Lifecycle
===================================================================================
===================================================================================
=====================================
2. React implements a "synthetic event" system that brings consistency and high
performance to react applications and interfaces.