if you want to remove an article from website contact us from top.

    what changes would appear in the component as soon as the state of the react component is changed?

    Mohammed

    Guys, does anyone know the answer?

    get what changes would appear in the component as soon as the state of the react component is changed? from screen.

    javascript

    I am watching Paul O Shannessy - Building React From Scratch And I understand the mounting process very well but I have hard day trying to understand how React update a component and its children The

    How does React update a component and its children after a state change?

    Ask Question Asked 10 months ago

    Modified 10 months ago

    Viewed 2k times 3

    I am watching Paul O Shannessy - Building React From Scratch

    And I understand the mounting process very well but I have hard day trying to understand how React update a component and its children

    The reconciler controls the update process by this method:

    function receiveComponent(component, element) {

    let prevElement = component._currentElement;

    if (prevElement === element) {

    return; }

    component.receiveComponent(element);

    }

    Component.receiveComponent

    receiveComponent(nextElement) {

    this.updateComponent(this._currentElement, nextElement);

    }

    and this is the Component.updateComponent method:

    updateComponent(prevElement, nextElement) {

    if (prevElement !== nextElement) {

    // React would call componentWillReceiveProps here

    }

    // React would call componentWillUpdate here

    // Update instance data

    this._currentElement = nextElement;

    this.props = nextElement.props;

    this.state = this._pendingState;

    this._pendingState = null;

    let prevRenderedElement = this._renderedComponent._currentElement;

    let nextRenderedElement = this.render();

    if (shouldUpdateComponent(prevRenderedElement, nextRenderedElement)) {

    Reconciler.receiveComponent(this._renderedComponent, nextRenderedElement);

    } }

    This is the part of the code that updates the component after state change, and i assume that it should update the children too, but i can't understand how this code achieves that, in the mounting process React instantiate components to dive deeper in the tree but this doesn't happen here, we need to find the first HTML element then we can change our strategy and update that HTML element in another place in the code, and I can't find any way to find any HTML elements this way.

    Finding the first HTML is the way to stop this endless recursion and logically this is what I expect from the code, to stop recursion the same way in the mounting process, but in mounting, this demanded component instantiation so we can delegate to the reconciler that will discover that we are dealing with a wrapper instance of an HTML element not a wrapper instance of a custom component then React can place that HTML element in the DOM.

    I can't understand how the code works in the update process. this code as I see won't dive deeper in the tree and I think won't update the children and can't let React find the first HTML element so React can update the DOM element, isn't it?

    This is the code repo on Github

    javascriptreactjsdomvirtual-dom

    Share

    edited Jan 15 at 6:50

    Yilmaz 21.9k9 9 gold badges 110 110 silver badges 146 146 bronze badges

    asked Jan 7 at 19:56

    Code Eagle 1,0381 1 gold badge 13 13 silver badges 31 31 bronze badges

    I have an answer to a similar question, How to set one component's state from another component in React, cheers! –

    HoldOffHunger Jan 7 at 20:16 1

    @HoldOffHunger thank you for reply but this is not even related to my question –

    Code Eagle Jan 7 at 20:20

    the render of the parent would be calling a createElement of each child, which would imply a render –

    Tiago Coelho Jan 14 at 16:00

    @TiagoCoelho can you please answer the question in detail using code snippets in the repo??..i only need code explaining because it is confusing to me –

    Code Eagle Jan 15 at 6:04 1 @CodeEagle done – Tiago Coelho Jan 18 at 23:20 Add a comment

    6 Answers

    2 +300

    I created a codesandbox to dig in

    Here is the codesandbox I created

    and here's a short recording of me opening the debugger and seeing the call stack.

    How it works

    Starting from where you left off, Component.updateComponent:

    updateComponent(prevElement, nextElement) {

    //...

    if (shouldUpdateComponent(prevRenderedElement, nextRenderedElement)) {

    Reconciler.receiveComponent(this._renderedComponent, nextRenderedElement);

    //...

    in the Component.updateComponent method Reconciler.receiveComponent is called which calls component.receiveComponent(element);

    Now, this component refers to this._renderedComponent and is not an instance of Component but of DOMComponentWrapper

    and here's the receiveComponent method of DOMComponentWrapper:

    receiveComponent(nextElement) {

    this.updateComponent(this._currentElement, nextElement);

    }

    updateComponent(prevElement, nextElement) {

    // debugger;

    this._currentElement = nextElement;

    this._updateDOMProperties(prevElement.props, nextElement.props);

    this._updateDOMChildren(prevElement.props, nextElement.props);

    }

    Then _updateDOMChildren ends up calling the children render method.

    here's a call stack from the codesandbox I created to dig in.

    How do we end up in DOMComponentWrapper

    in the Component's mountComponent method we have:

    let renderedComponent = instantiateComponent(renderedElement);

    this._renderedComponent = renderedComponent;

    स्रोत : stackoverflow.com

    React.Component – React

    A JavaScript library for building user interfaces

    React.Component

    This page contains a detailed API reference for the React component class definition. It assumes you’re familiar with fundamental React concepts, such as Components and Props, as well as State and Lifecycle. If you’re not, read them first.

    Overview

    React lets you define components as classes or functions. Components defined as classes currently provide more features which are described in detail on this page. To define a React component class, you need to extend React.Component:

    class Welcome extends React.Component {

    render() { return

    Hello, {this.props.name}

    ;

    } }

    The only method you must define in a React.Component subclass is called render(). All the other methods described on this page are optional.

    We strongly recommend against creating your own base component classes. In React components, code reuse is primarily achieved through composition rather than inheritance.

    Note:

    React doesn’t force you to use the ES6 class syntax. If you prefer to avoid it, you may use the create-react-class module or a similar custom abstraction instead. Take a look at Using React without ES6 to learn more.

    The Component Lifecycle

    Each component has several “lifecycle methods” that you can override to run code at particular times in the process. You can use this lifecycle diagram as a cheat sheet. In the list below, commonly used lifecycle methods are marked as bold. The rest of them exist for relatively rare use cases.

    Mounting

    These methods are called in the following order when an instance of a component is being created and inserted into the DOM:

    constructor()

    static getDerivedStateFromProps()

    render()componentDidMount()

    Note:

    This method is considered legacy and you should avoid it in new code:

    UNSAFE_componentWillMount()

    Updating

    An update can be caused by changes to props or state. These methods are called in the following order when a component is being re-rendered:

    static getDerivedStateFromProps()

    shouldComponentUpdate()

    render()

    getSnapshotBeforeUpdate()

    componentDidUpdate()

    Note:

    These methods are considered legacy and you should avoid them in new code:

    UNSAFE_componentWillUpdate()

    UNSAFE_componentWillReceiveProps()

    Unmounting

    This method is called when a component is being removed from the DOM:

    componentWillUnmount()

    Error Handling

    These methods are called when there is an error during rendering, in a lifecycle method, or in the constructor of any child component.

    static getDerivedStateFromError()

    componentDidCatch() Other APIs

    Each component also provides some other APIs:

    setState() forceUpdate() Class Properties defaultProps displayName

    Instance Properties

    props state Reference

    Commonly Used Lifecycle Methods

    The methods in this section cover the vast majority of use cases you’ll encounter creating React components. For a visual reference, check out this lifecycle diagram.

    render() render()

    The render() method is the only required method in a class component.

    When called, it should examine this.props and this.state and return one of the following types:

    React elements. Typically created via JSX. For example, and are React elements that instruct React to render a DOM node, or another user-defined component, respectively.Arrays and fragments. Let you return multiple elements from render. See the documentation on fragments for more details.Portals. Let you render children into a different DOM subtree. See the documentation on portals for more details.String and numbers. These are rendered as text nodes in the DOM.Booleans or null or undefined. Render nothing. (Mostly exists to support return test && pattern, where test is boolean).

    The render() function should be pure, meaning that it does not modify component state, it returns the same result each time it’s invoked, and it does not directly interact with the browser.

    If you need to interact with the browser, perform your work in componentDidMount() or the other lifecycle methods instead. Keeping render() pure makes components easier to think about.

    Note

    render() will not be invoked if shouldComponentUpdate() returns false.

    constructor() constructor(props)

    If you don’t initialize state and you don’t bind methods, you don’t need to implement a constructor for your React component.

    The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in the constructor, which can lead to bugs.

    Typically, in React constructors are only used for two purposes:

    Initializing local state by assigning an object to this.state.

    Binding event handler methods to an instance.

    You should not call setState() in the constructor(). Instead, if your component needs to use local state, assign the initial state to this.state directly in the constructor:

    स्रोत : reactjs.org

    React.js MCQ (Multiple Choice Questions)

    Top 35+ Most Asked React.js MCQ (Multiple Choice Questions) with ReactJS Tutorial, ReactJS Introduction, ReactJS Features, ReactJS Installation, Pros and Cons of ReactJS, AngularJS vs ReactJS, Reactnative vs ReactJS, ReactJS Router, ReactJS Flux Concept, ReactJS Animations, ReactJS Discussion, ReactJS Quick Guide, etc.

    Home React Native ReactJS TypeScript JavaScript Framework7 COA HTML CSS Selenium Servlet JSP jQuery Quiz Projects Interview Q Comment

    Top 35+ Most Asked React.js MCQ

    1) Which of the following is the correct name of React.js?

    React React.js ReactJS All of the above

    2) Which of the following are the advantages of React.js?

    React.js can increase the application's performance with Virtual DOM.

    React.js is easy to integrate with other frameworks such as Angular, BackboneJS since it is only a view library.

    React.js can render both on client and server side.

    All of the above

    3) Which of the following is not a disadvantage of React.js?

    React.js has only a view layer. We have put your code for Ajax requests, events and so on.

    The library of React.js is pretty large.

    The JSX in React.js makes code easy to read and write.

    The learning curve can be steep in React.js.

    4) Which of the following command is used to install create-react-app?

    npm install -g create-react-app

    npm install create-react-app

    npm install -f create-react-app

    install -g create-react-app

    5) What of the following is used in React.js to increase performance?

    Original DOM Virtual DOM Both A and B. None of the above.

    6) A class is a type of function, but instead of using the keyword function to initiate it, which keyword do we use?

    Constructor Class Object DataObject

    7) Which of the following acts as the input of a class-based component?

    Class Factory Render Props

    8) Which of the following keyword is used to create a class inheritance?

    Create Inherits Extends This

    9) What would be the output of the following example?

    var Helloword=(props)=>

    ReactDOM.render(

    { return( Hello World 1 Hello World 2 ); }

    ,mountNode)

    Hello World 1 Hello World 2

    Hello World 1 Hello World 2

    Error

    10) What is the default port where webpack-server runs?

    3000 8080 3030 6060

    11) How many numbers of elements a valid react component can return?

    1 2 4 5

    12) What is the declarative way to render a dynamic list of components based on values in an array?

    Using the reduce array method

    Using the component

    Using the Array.map() method

    With a for/while loop

    13) How many ways of defining your variables in ES6?

    1 3 4 5

    14) What is a state in React?

    A permanent storage.

    Internal storage of the component.

    External storage of the component.

    None of the above.

    15) What are the two ways to handle data in React?

    State & Props

    Services & Components

    State & Services State & Component

    16) In which of the following directory React.js components are saved?

    Inside the js/components/

    Inside the vendor/components/

    Inside the external/components/

    Inside the vendor/

    17) Which of the following is a must API for every React.js component?

    SetinitialComponent renderComponent render All of the above

    18) Which of the following option is correct in the case of the Babel?

    Babel is a Compiler.

    Babel is a Transpilar.

    None of the above.

    Both A and B are correct.

    19) Does React.js create a VIRTUAL DOM in the memory?

    TRUE FALSE

    Can be true or false

    Cannot say

    20) What is the use of "webpack" command in React.js?

    The "webpack" command is used to transpile all the JavaScript down into one file.

    It runs React local development server.

    It is a module bundler.

    None of the above.

    21) Which of the following is used to pass data to a component from outside in React.js?

    SetState

    Render with arguments

    Props PropTypes

    22) What does ES6 stand for?

    ECMAScript 6 ECMA 6 ECMAJavaScript 6 EJavaScript 6

    23) Which of the following function is used to change the state of the React.js component?

    this.setState this.setChangeState this.State{} None of the above.

    24) Which of the following method refers to the parent class in React.js?

    inherits() self() super() this()

    25) What will happen if you render an input element with disabled = {false}?

    It will be rendered as disabled

    It will not be rendered at all

    It will be rendered as enabled

    You cannot set it false.

    26) Which of the following function is called to render HTML to the web page in React?

    render() render() ReactDOM_render() render()

    27) Which of the following lifecycle events React components have at the highest level?

    Destruction Initialization

    State/Property Updates

    All of the above.

    28) Why is the usage of setState?

    Invoke code after the setState operation is done.

    Replace the state completely instead of the default merge action.

    Access the previous state before the setState operation.

    None of the above.

    29) Which of the following best defines the "key" prop?

    स्रोत : www.javatpoint.com

    Do you want to see answer or more ?
    Mohammed 14 day ago
    4

    Guys, does anyone know the answer?

    Click For Answer