Skip to main content

React

Question : What are the different types of components?
Answer : There are two types of component, function component and the class component,
1. Functional component : They are also called as the stateless component, they accept the props and returns the DOM. They are the most basic way of defining the component in the React.

2. Class Component : They are also called as the stateful component. They are more versatile way of defining the component where they have the state and props, the difference between state and props is the state can be changed but props can not be changed once defined.

Question : Describe the following webpack.config.js
var config = {
    entry: './main.js',
    output: {
        path: __dirname,
        filename: 'index.js',
    },
    devServer: {
        inline: true,
        port: 8080
    }, module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015', 'react']
                }
            }
        ]
    }
}
module.exports = config;

Answer : Here  We are setting webpack entry point to be main.js . Output path is the place where bundled app will be served. We are also setting the development server to 8080  port. You can choose any port you want. And lastly, we are setting babel loaders to search for js  files, and use es2015  and react presets that we installed before as babel-preset-react and babel-preset-es2015.

Question : Write a basic HelloWorld Basic Functional Component?
Answer :
const helloComponent = (props) => (
            <div>
                Hello World!!!
            </div>
);

alternatively can be written in this way as well
const helloComponent = (props) => { 
     return (
            <div>
                Hello World!!!
            </div>

    );
}

Question : Write a basic HelloWorld Class Component?
Answer :
class App extends React.Component {
    render() {
        return (
            <div>
                Hello World!!!
            </div>
        );
    }
}

Question : How does the component communicate with the outside world?
Answer : Each react component can accepts the props as it's argument and that is how we can communicate and also instructs the component to have behavior based on the props.

Question : What are the two main properties of the React Class Component?
Answer : props and state are to main Instance Properties,

props : this.props contains the props that were defined by the caller of this component. See Components and Props for an introduction to props.
In particular, this.props.children is a special prop, typically defined by the child tags in the JSX expression rather than in the tag itself.

state : The state contains data specific to this component that may change over time. The state is user-defined, and it should be a plain JavaScript object.
If you don’t use it in render(), it shouldn’t be in the state. For example, you can put timer IDs directly on the instance.
Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.

Question : Describe Virtual DOM in terms of React?
Answer : React is a library that is designed to update the browser DOM for us. We no longer have to be concerned with the complexities associated with building performant SPAs because React can do that for us. With React, we do not interact with the DOM API directly. Instead, we interact with a virtual DOM, or set of instructions that React will use to construct the UI and interact with the browser.5
The virtual DOM is made up of React elements, which conceptually seem similar to HTML elements, but are actually JavaScript objects. It is much faster to work directly with JavaScript objects than it is to work with the DOM API. We make changes to a JavaScript object, the virtual DOM, and React renders those changes for us using the DOM API as efficiently as possible.

Question : What is React Elements?
Answer : The browser DOM is made up of DOM elements. Similarly, the React DOM is made up of React elements. DOM elements and React elements may look the same, but they are actually quite different. A React element is a description of what the actual DOM element should look like. In other words, React elements are the instructions for how the browser DOM should be created.
e.g. React.createElement("h1", null, "Baked Salmon");
The first argument defines the type of element that we wish to create. In this case, we want to create a heading-one element. The third argument represents the element’s children, any nodes that are inserted between the opening and closing tag. The second argument represents the element’s properties. This h1 currently does not have any properties.

What is ReactDOM?
Answer : ReactDOM contains the tools necessary to render React elements in the browser. ReactDOM is where we will find the render method as well as the renderToString and renderToStaticMarkup methods that are used on the server. These will be discussed in greater detail in Chapter 12. All the tools necessary to generate HTML from the virtual DOM are found in this library.
All of the DOM rendering functionality in React has been moved to ReactDOM because we can use React to build native applications as well. The browser is just one target for React.

What is React DOM Children?
Answer : ReactDOM allows you to render a single element to the DOM. React tags this as data-reactroot. All other React elements are composed into a single element using nesting.
React renders child elements using props.children. We could render other React elements as children too, creating a tree of elements. This is why we use the term component tree. The tree has one root component from which many branches grow.
E.g.
React.createElement(
    "ul",
    null,
    React.createElement("li", null, "1 lb Salmon"),
    React.createElement("li", null, "1 cup Pine Nuts"),
    React.createElement("li", null, "2 cups Butter Lettuce"),
    React.createElement("li", null, "1 Yellow Squash"),
    React.createElement("li", null, "1/2 cup Olive Oil"),
    React.createElement("li", null, "3 cloves of Garlic")
)

Comments

Popular posts from this blog

Collections JAVA

Collection Question:Comparable and Comparator? Comparable Comparator Comparable provides single sorting sequence. In other words, we can sort the collection on the basis of single element such as id or name or price etc. Comparator provides multiple sorting sequence. In other words, we can sort the collection on the basis of multiple elements such as id, name and price etc. Comparable affects the original class i.e. actual class is modified. Comparator doesn't affect the original class i.e. actual class is not modified. Comparable provides compareTo() method to sort elements. Comparator provides compare() method to sort elements. Comparable is found in java.lang package. Comparator is found in java.util package. We can sort the list elements of Comparable type byCollections.sort(List) method. We can sort the list elements of Comparator type   byCollections.sort(List,...

NodeJS

Question : Why You should use Node JS? Answer :  Following are the major factor influencing the use of the NodeJS Popularity : The popularity can be important factor, as it has more user base and hence solution  of any common problem faced by developer can found easily online, without any professional help. JavaScript at all levels of the stack :  A common language for frontend and backend offers several potential benefits: The same programming staff can work on both ends of the wire Code can be migrated between server and client more easily Common data formats (JSON) exist between server and client Common software tools exist for server and client Common testing or quality reporting tools for server and client When writing web applications, view templates can be used on both sides Leveraging Google's investment in V8 Engine. Leaner, asynchronous, event-driven model Microservice architecture Question : example of node JS code? Answer :  const fs = require('fs'); co...

Graphql

Question : Why GraphQL? Answer : There are various reason why we benefit from using the graphQL Clean API between backends and frontends Less communication overhead  No more time spent writing API documentation No more time spent trying to figure out an API Great tooling for your API Supports multiple clients and multiple server interactions. Question : Describe GraphQL in nutshell? Answer : GraphQL is Language and Runtime both, Language gives us the tool to specify the queries, mutation, subscription, fragment. Runtime describe the validation, Type Systems, execution and introspection. Question : Describe the overall interaction cycle of the graphQL? Answer : These are the basic steps carried during the interaction between client and the server Read inputs from Interface, Parse into the Abstract Syntax Tree,  invokes its resolver function, then it either gets the scalar data or else invokes another internal resolver function. Data returned from resolver function are then m...