深入理解 useEffect

发布于 2025-02-19 11:01:01 字数 3217 浏览 9 评论 0

class Example extends Component {
  constructor (props) {
    super(props);
    this.state = {
      count: 0
    }
  }
  render() { 
    return (
      <div>
        <p>点击{this.state.count}次</p>
        <button onClick={() => this.setState({count: this.state.count + 1})}>
          点击
        </button>
      </div>
    );
  }
}

类组件和函数组件

function Example () {
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>点击{count}次</p>
      <button onClick={() => setCount(count + 1)}>
        点击
      </button>
    </div>
  )
}

生命周期上的功能转移到 useEffect 里面实现

class Example extends Component {
  constructor (props) {
    super(props);
    this.state = {
      count: 0
    }
  }
  componentDidMount() {
    document.title = `点击${this.state.count}次`
  }
  componentDidUpdate(prevProps, prevState) {
    document.title = `点击${this.state.count}次`
  }
  render() { 
    return (
      <div>
        <p>点击{this.state.count}次</p>
        <button onClick={() => this.setState({count: this.state.count + 1})}>
          点击
        </button>
      </div>
    );
  }
}

function Example () {
  const [count, setCount] = useState(0);
  useEffect(() => {
    document.title = `点击${count}次`
  })
  return (
    <div>
      <p>点击{count}次</p>
      <button onClick={() => setCount(count + 1)}>
        点击
      </button>
    </div>
  )
}
  1. 在 class 组件中,我们可以通过在 componentDidUpdate 中添加对 prevPropsprevState 的比较逻辑解决
  2. 你可以通知 React 跳过对 effect 的调用,只要传递数组作为 useEffect 的第二个可选参数即可
class Example extends Component {
  constructor (props) {
    super(props);
    this.state = {
      count: 0
    }
  }
  componentDidMount() {
    document.title = `点击${this.state.count}次`
  }
  componentDidUpdate(prevProps, prevState) {
    if (prevState.count !== this.state.count) {
      document.title = `点击${this.state.count}次`
    }
  }
  render() { 
    return (
      <div>
        <p>点击{this.state.count}次</p>
        <button onClick={() => this.setState({count: this.state.count + 1})}>
          点击
        </button>
      </div>
    );
  }
}

function Example () {
  const [count, setCount] = useState(0);
  useEffect(() => {
    document.title = `点击${count}次`
  }, [count])
  return (
    <div>
      <p>点击{count}次</p>
      <button onClick={() => setCount(count + 1)}>
        点击
      </button>
    </div>
  )
}

传入 [] 作为第二个参数更接近大家更熟悉的 componentDidMountcomponentWillUnmount 思维模式

组件加载启动一个定时器,组件销毁清除定时器

function Example () {
  const [count, setCount] = useState(0);
  useEffect(() => {
    const timer = setInterval(() => {
      console.log(1)
    }, 1000)
    return () => clearInterval(timer)
  }, [])
  return (
    <div>
      <p>点击{count}次</p>
      <button onClick={() => setCount(count + 1)}>
        点击
      </button>
    </div>
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

卖梦商人

暂无简介

文章
评论
27 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文