簡體   English   中英

如何在 React 中將 Class 組件轉換為函數式組件?

[英]How to Convert a Class Component to a Functional Component in React?

我從未使用過 React Class 組件,並且想在某些事件發生時拍攝五彩紙屑。 我對 Class 組件感到困惑。 希望你能幫助解決這個問題。 通過創建箭頭函數並刪除此關鍵字進行了嘗試。但我不明白如何轉換 getInstance() function 甚至為什么它在那里?

import React, { Component } from 'react';
import ReactCanvasConfetti from 'react-canvas-confetti';

export default class Confetti extends Component {
    getInstance = (instance) => {
        // saving the instance to an internal property
        this.confetti = instance;
    }

    onClickDefault = () => {
        // starting the animation
        this.confetti();
    }

    onClickCustom = () => {
        // starting the animation with custom settings
        this.confetti({ particleCount: Math.ceil(Math.random() * 1000), spread: 180 });
    }

    onClickCallback = () => {
        // calling console.log after the animation ends
        this.confetti().then(() => {
            console.log('do something after animation');
        });
    }

    onClickReset = () => {
        // cleaning the canvas
        this.confetti.reset();
    }

    render() {
        const style = {
            position: 'fixed',
            width: '100%',
            height: '100%',
            zIndex: -1

        };
        const stylediv = {
            position: 'fixed',
            width: '100%',
            height: '100%',
            zIndex: 300000

        };

        return (
            <>
                <div style={stylediv}>

                    <ReactCanvasConfetti
                        // set the styles as for a usual react component
                        style={style}
                        // set the class name as for a usual react component
                        className={'yourClassName'}
                        // set the callback for getting instance. The callback will be called after initialization ReactCanvasConfetti component
                        refConfetti={this.getInstance}
                    />

                    <button onClick={this.onClickDefault}>Fire with default</button>
                    <button onClick={this.onClickCustom}>Fire with custom</button>
                    <button onClick={this.onClickCallback}>Fire with callback</button>
                    <button onClick={this.onClickReset}>Reset</button>
                </div>
            </>
        );
    }
}

我正在嘗試創建上述 Class 組件的功能組件

import React, { useRef } from 'react';
import ReactCanvasConfetti from 'react-canvas-confetti';

const Confetti = () => {
    const Ref = useRef()
    const getInstance = (instance) => {
      if (Ref.current) {
         Ref.current.confetti = instance
      }
   }
   const onClickDefault = () => {
      Ref.current.confetti();
   }
   const onClickCustom = () => {
      Ref.current.confetti({ particleCount: Math.ceil(Math.random() * 1000), 
      spread: 180 });
   }

   const onClickCallback = () => {
      Ref.current.confetti().then(() => {
          console.log('do something after animation');
      });
   }
   const onClickReset = () => {
      Ref.current.confetti.reset();
   }

   const style = {
    position: 'fixed',
    width: '100%',
    height: '100%',
    zIndex: -1
  };
  const stylediv = {
    position: 'fixed',
    width: '100%',
    height: '100%',
    zIndex: 300000

  };
  return (
    <>
        <div ref={Ref} style={stylediv}>

            <ReactCanvasConfetti
                style={style}
                className={'yourClassName'}
                refConfetti={getInstance}
            />
            <button onClick={onClickDefault}>Fire with default</button>
            <button onClick={onClickCustom}>Fire with custom</button>
            <button onClick={onClickCallback}>Fire with callback</button>
            <button onClick={onClickReset}>Reset</button>
        </div>
    </>
);

} 導出默認五彩紙屑

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM