React drag and drop to add new components

Source: Internet
Author: User
Tags react drag and drop

1. Because I am not very familiar with the react framework idea, I am used to solving problems through Js.

2. this drag-and-drop operation adds a new component. js can directly append an element to an element on A Webpage Through Dom operations, but react obviously cannot. For example, add a custom widget or chart dynamically.

3. Solution: Define an array this. State = {chartlist: []}. After you drag a component of a certain type, add a component (?) to the array (?). Update the component and output it through render.

4. This method encountered a problem when you directly add a component to the array

Const newline = <reachartssimplelinechart/>

Const listline = This. state. chartlist. Push (newline );

This. setstate ({

Chartlist: listline

})

This method will cause an error. It will fail only after the first addition is successful, and this and setstate will not be updated.

5. Solution: Use immutability-helper

Const listline = Update (chartlist, {$ push: [newline]});

This. setstate ({

Chartlist: listline

})

In this way, the instance is successfully added.

Principle: immutable data

React drag and drop to add new components

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.