简体   繁体   English

从React中的事件处理程序中删除动态DOM节点的正确方法是什么?

[英]what's the right way to remove dynamic DOM Nodes from an event handler in React?

DOM manipulation are the simplest stuff offered by a JS library. DOM操作是JS库提供的最简单的东西。 But in react everything dynamic has to be in the state. 但在反应中,动态必须处于状态。 It becomes super diificult to just add or remove simple DOM nodes. 只添加或删除简单的DOM节点变得非常困难。 Maybe this are the cons of React or probably I don't know any other way. 也许这是React的缺点,或者我可能不知道其他任何方式。

Take a look at this-- 看看这个 -

class Ripple extends React.Component {
    state = {
        ripples: []
    }

    render() {
        const { className } = this.props;

        return (
            <div onClick={this.rippleNow} className={`${className} material__ripple`}>

            {this.props.children}

            {this.state.ripples.map((Item, index) => (
                <Item key={index} />
            ))}

            </div>
        );
    }

    rippleNow = ({ currentTarget, clientX, clientY }) => {
        const { x, y } = currentTarget.getBoundingClientRect(),
              { offsetHeight, offsetWidth } = currentTarget,
              dia = Math.min(offsetHeight, offsetWidth, 100);


        const styles = {
            top: (clientX - x) - dia / 2,
            left: (clientY - y) - dia / 2,
            width: dia,
            height: dia
        }

        const Wave = () => (
          <div onAnimationEnd={this.removeRipple} style={styles}></div>
        );

        this.setState(prev => {
           ripples: [...prev.ripples, Wave]
       });
    }

    removeRipple = (e) => {
        // How will i remove that element?
        // Or is there any other way to do the same?
    }
 }

How will i remove the element from the state? 我将如何从州中删除元素? Or is there any other way to add or remove DOM Elements in react . 或有任何其他方式在反应中添加或删除DOM元素。 As react doesn't allow modifying DOM directly . 由于反应不允许直接修改DOM。

I am in a real problem . 我是一个真正的问题。 Thanks to those who'll even try to help . 感谢那些甚至试图提供帮助的人。

You generally shouldn't be storing elements themselves in the state. 您通常不应将元素本身存储在状态中。 Where you are storing <Wave /> in the state, you could much more efficiently store currentTarget , clientX , etc and generate the react element on the render() cycle. 在状态中存储<Wave />的地方,可以更有效地存储currentTargetclientX等,并在render()循环中生成react元素。 From the archived react docs 来自存档的反应文档

What shouldn't go in the state? 什么不该去州? - React components: Build them in render() based on underlying props and state data. - React组件:根据底层的props和状态数据在render()中构建它们。

The react material-ui library has a circular material ripple that you can look at if you need some examples of how this is done in practice. 反应材料-ui库具有圆形材料波纹,如果您需要在实践中如何完成此操作的一些示例,您可以查看它们。 Here is the source for their ripple component, and you can see a demo by clicking some of the buttons on this page . 以下是其纹波组件的来源 ,您可以通过单击此页面上的某些按钮来查看演示。

The gist of it is that they only allow a fixed number of ripple elements, and use a ReactTransitionGroup to handle starting and aborting the animations. 它的要点是它们只允许固定数量的纹波元素,并使用ReactTransitionGroup来处理启动和中止动画。 Each time a ripple is added, the first element of the ripple array is removed and a new item is added. 每次添加纹波时,纹波阵列的第一个元素都会被移除,并添加一个新项目。

However, if you don't care about any of that - you can fix your code by just removing the first ripple from the array when you call removeRipple . 但是,如果你不关心任何一个 - 你可以通过在调用removeRipple时从数组中删除第一个纹波来修复代码。 Since you're always adding the newest ripple to the end of the array, the first one will always be the oldest one. 由于您总是将最新的纹波添加到数组的末尾,因此第一个纹波始终是最旧的纹波。

removeRipple = (e) => {

    // create a new element with the first element removed
    const [, ...shiftedRipples] = this.state.ripples;

    // update the state
    this.setState({ ripples: shiftedRipples });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ReactJS组件中定义事件处理程序的正确方法是什么? - What's the right way of defining event handler in ReactJS Component? 在React中编写事件处理程序的最佳方法是什么? - What is the best way to write event handler in React? React 的 useEffect 和 DOM 事件处理程序之间的执行顺序 - Execution order between React's useEffect and DOM event handler 如何使用事件处理程序从dom树中删除元素 - how to remove element from dom tree with the event handler 用参数处理事件处理函数的更好方法是什么? - What's the better way to handle event handler function with parameters? 如何装饰DO​​M节点的事件处理程序? - How to decorate a DOM node's event handler? 如何在 React 的事件处理函数中访问 DOM 元素的属性和值? - How to access DOM element's attributes and value inside an event handler function in React? 在DOM事件处理程序中直接调用作用域函数的最短方法是什么 - What is the shortest way to call a scope function directly within a DOM event handler 在 react-router-dom 中分离路由的最佳方法是什么? - What's the best way to separate the routes in react-router-dom? 事件处理程序的正确名称是什么? onClick 还是 handleClick? - What is the right name of event handler? onClick or handleClick?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM