React/Redux/Webpack/React Native

Ace your homework & exams now with Quizwiz!

How to Pass Data Between React Components?

1. Parent to Child — Use Prop I can pass it as a prop to the child when I instantiate it within the parent. In my example, if I need to pass something from the App to the ToDoList: class App extends React.Component { render() { [... somewhere in here I define a variable listName which I think will be useful as data in my ToDoList component...] return ( <div> <InputBar/> <ToDoList listNameFromParent={listName}/> </div> ); } } Now in the ToDoList component, if I use this.props.listNameFromParent I will have access to that data. 2. Child to Parent — Use a callback and states - Callback in my parent which takes the data I need in as a parameter. - Pass that callback as a prop to the child (see above). - Call the callback using this.props.[callback] in the child (insert your own name where it says [callback] of course), and pass in the data as the argument.

How different is React's ES6 syntax when compared to ES5?

1. require vs import // ES5 var React = require('react'); // ES6 import React from 'react'; cf. NodeJS still requires "require" instead of "import" 2. export vs exports // ES5 module.exports = Component; // ES6 export default Component; 3. component and function // ES5 var MyComponent = React.createClass({ render: function() { return <h3>Hello Edureka!</h3> ; } }); // ES6 class MyComponent extends React.Component { render() { return <h3>Hello Edureka!</h3> ; } } 4. props // ES5 var App = React.createClass({ propTypes: { name: React.PropTypes.string }, render: function() { return <h3>Hello, {this.props.name}!</h3> ; } }); // ES6 class App extends React.Component { render() { return <h3>Hello, {this.props.name}!</h3> ; } } 5. state // ES5 var App = React.createClass({ getInitialState: function() { return { name: 'world' }; }, render: function() { return <h3>Hello, {this.state.name}!</h3> ; } }); // ES6 class App extends React.Component { constructor() { super(); this.state = { name: 'world' }; } render() { return <h3>Hello, {this.state.name}!</h3> ; } }

What is virtual DOM?

A virtual DOM is a lightweight JavaScript object which originally is just the copy of the real DOM. It is a node tree that lists the elements, their attributes and content as Objects and their properties. React's render function creates a node tree out of the React components. It then updates this tree in response to the mutations in the data model which is caused by various actions done by the user or by the system. This Virtual DOM works in three simple steps. 1. Whenever any underlying data changes, the entire UI is re-rendered in Virtual DOM representation. 2. Then the difference between the previous DOM representation and the new one is calculated. 3. Once the calculations are done, the real DOM will be updated with only the things that have actually changed.

Compare Webpack vs Grunt

Comparing Webpack vs Grunt, the first of those offers more flexibility and advanced functionality for modern front-end projects. It comes with a functional core and can be extended using particular loaders and plugins. Essentially it is used for bundling JavaScript modules with dependencies into files, but for complex JavaScript applications with lots of non-code assets (images, fonts, CSS, etc.) it can provide great benefits. Talking about Webpack vs Gulp vs Grunt performance, the two latter look into a defined path for files that match your configuration, while the Webpack analyzes the whole project. It looks through all the dependencies, processes them with loaders and produces a bundled JS file.

What are Components in React?

Components are the building blocks of a React application's UI. These components split up the entire UI into small independent and reusable pieces. Then it renders each of these components independent of each other without affecting the rest of the UI.

What is the purpose of render in React?

Each React component must have a render() mandatorily. It returns a single React element which is the representation of the native DOM component. If more than one HTML element needs to be rendered, then they must be grouped together inside one enclosing tag such as <form>, <group>,<div> etc. This function must be kept pure i.e., it must return the same result each time it is invoked.

What are the React features/advantages/limitations?

Features It uses the virtual DOM instead of the real DOM. It uses server-side rendering. It follows uni-directional data flow or data binding. Advantages It increases the application's performance It can be conveniently used on the client as well as server side Because of JSX, code's readability increases React is easy to integrate with other frameworks like Meteor, Angular, etc Using React, writing UI test cases become extremely easy Limitations React is just a library, not a full-blown framework Its library is very large and takes time to understand It can be little difficult for the novice programmers to understand Coding gets complex as it uses inline templating and JSX

What is JSX and why can't browsers read JSX?

JSX is a shorthand for JavaScript XML. This is a type of file used by React which utilizes the expressiveness of JavaScript along with HTML like template syntax. This makes the HTML file really easy to understand. This file makes applications robust and boosts its performance. Browsers can only read JavaScript objects but JSX in not a regular JavaScript object. Thus to enable a browser to read JSX, first, we need to transform JSX file into a JavaScript object using JSX transformers like Babel and then pass it to the browser.

What are the differences between props and state in React?

Props are read-only components which must be kept pure i.e. immutable. They are always passed down from the parent to the child components throughout the application. A child component can never send a prop back to the parent component. This help in maintaining the unidirectional data flow and are generally used to render the dynamically generated data. States are local, the source of data and mutable unlike the props and can change at any time in the component's lifecycle. They are accessed via this.state(). Also, state of a component can be updated using this.setState(). Conditions State Props 1. Receive initial value from parent component Yes Yes 2. Parent component can change value No Yes 3. Set default values inside component Yes Yes 4. Changes inside component Yes No 5. Set initial value for child components Yes Yes 6. Changes inside child components No Yes

How is React different from Angular?

React vs Angular TOPIC REACT ANGULAR 1. ARCHITECTURE Only the View of MVC Complete MVC 2. RENDERING Server-side rendering Client-side rendering cf. Server-side rendering: it works by converting HTML files in the server into usable information for the browser and getting back the fully rendered HTML and displays it on the screen. (entire new page would be rendered, and not just the new content.). It's great for SEO as your content is present before you get it, so search engines are able to index it and crawl it just fine. Client-side rendering: if you were to click on the link the page to load more content, the browser will not make another request to the server. You are rendering items with the browser, so it will instead use JavaScript to load the new content and it will make sure that only the new content is rendered. Everything else will be left alone. The server is now only responsible for loading the boilerplate. Everything else is handled by a client-side JavaScript library. Since the content is not rendered until the page is loaded on the browser, SEO for the website will take a hit. 3. DOM Uses virtual DOM Uses real DOM 4. DATA BINDING One-way data binding Two-way data binding cf. In two way data binding, the view is updated when the state changes, and vice versa. It can be achieved using a ngModel directive in Angular. The below syntax shows the data binding using (ngModel), which is basically the combination of both the square brackets of property binding and parentheses of the event binding. In one way data binding, new data always enters into the store through actions using dispatcher. View components subscribe to the stores and automatically re-render themselves using the new data. The data flows in a single direction from parent to child making it much more predictable. As you have a STORE as a single source of truth you can easily debug the app in case something goes wrong giving better control over it. 5. DEBUGGING Compile time debugging Runtime debugging 6. AUTHOR Facebook Google

Differentiate between Real DOM and Virtual DOM.

Real DOM Virtual DOM 1. It updates slow. 1. It updates faster. 2. Can directly update HTML. 2. Can't directly update HTML. 3. Creates a new DOM if element updates. 3. Updates the JSX if element updates. 4. DOM manipulation is very expensive. 4. DOM manipulation is very easy. 5. Too much of memory wastage. 5. No memory wastage.

Differentiate between stateful and stateless components.

Stateful Component Stateless Component 1. Stores info about component's state change in memory 1. Calculates the internal state of the components 2. Have authority to change state 2. Do not have the authority to change state 3. Contains the knowledge of past, current and possible future changes in state 3. Contains no knowledge of past, current and possible future state changes 4. Stateless components notify them about the requirement of the state change, then they send down the props to them. 4. They receive the props from the Stateful components and treat them as callback functions.

What is Redux for?

The entire state of the application will be represented by one JavaScript object (state tree). This means we will never directly manipulate values of our state tree. State will be updated / changed by dispatched actions. Inside a redux app there is one particular function that takes the previous state and the action being dispatched, and returns the next state of the application. An event from the client side triggers dispatch method with action and reducer updates state accordingly and then UI re-renders accordingly.

What is arrow function in React? How is it used?

This allows to bind the context of the components properly since in ES6 auto binding is not available by default. Arrow functions are mostly useful while working with the higher order functions. //General way render() { return( <MyInput onChange={this.handleChange.bind(this) } /> ); } //With Arrow Function render() { return( <MyInput onChange={ (e) => this.handleOnChange(e) } /> ); }

What's webpack?

webpack is a module bundler for javascript applications. Webpack recursively builds every module in your application, then packs all those modules into a small number of bundles.


Related study sets

Chromatography Practical Questions

View Set

Chapter 51: Care of Patients with Musculoskeletal Trauma

View Set

Chapter 25, The Child with Gastrointestinal Dysfunction peds

View Set

Lipscomb Psychology 2301 Chapter 9 Themes and Variations 9E Intelligence

View Set

nonpharmacologic methods of pain relief - sherpath

View Set

AP Chemistry | Unit 3 Progress Check: MCQ

View Set