在日常开发中,React 18 Suspense SSR 流式渲染 is being used more and more frequently. This article systematically explains its usage, principles, and optimization strategies.
Quick Start
We can improve it in the following ways:
import { useRef, useEffect, useState } from 'react'
function useIntersectionObserver(options = {}) {
const [isVisible, setIsVisible] = useState(false)
const ref = useRef(null)
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
setIsVisible(entry.isIntersecting)
}, { threshold: 0.1, ...options })
const el = ref.current
if (el) observer.observe(el)
return () => { if (el) observer.unobserve(el) }
}, [])
return [ref, isVisible]
}
This approach has been running stably in production for over six months and has been practically validated.
Internal Principles
Let's start with the basic implementation:
import { useRef, useEffect, useState } from 'react'
function useIntersectionObserver(options = {}) {
const [isVisible, setIsVisible] = useState(false)
const ref = useRef(null)
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
setIsVisible(entry.isIntersecting)
}, { threshold: 0.1, ...options })
const el = ref.current
if (el) observer.observe(el)
return () => { if (el) observer.unobserve(el) }
}, [])
return [ref, isVisible]
}
This code demonstrates the basic usage. In real projects, you also need to consider error handling and edge cases.
Business Practice
Building on this foundation, we can further optimize:
import { useRef, useEffect, useState } from 'react'
function useIntersectionObserver(options = {}) {
const [isVisible, setIsVisible] = useState(false)
const ref = useRef(null)
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
setIsVisible(entry.isIntersecting)
}, { threshold: 0.1, ...options })
const el = ref.current
if (el) observer.observe(el)
return () => { if (el) observer.unobserve(el) }
}, [])
return [ref, isVisible]
}
This pattern is very practical in large projects and can significantly reduce maintenance costs.
Performance Comparison
实际项目中的用法会更复杂一些:
import { useRef, useEffect, useState } from 'react'
function useIntersectionObserver(options = {}) {
const [isVisible, setIsVisible] = useState(false)
const ref = useRef(null)
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
setIsVisible(entry.isIntersecting)
}, { threshold: 0.1, ...options })
const el = ref.current
if (el) observer.observe(el)
return () => { if (el) observer.unobserve(el) }
}, [])
return [ref, isVisible]
}
Through this approach, both the testability and scalability of the code are improved.
Troubleshooting
Here is a complete example:
import { useRef, useEffect, useState } from 'react'
function useIntersectionObserver(options = {}) {
const [isVisible, setIsVisible] = useState(false)
const ref = useRef(null)
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
setIsVisible(entry.isIntersecting)
}, { threshold: 0.1, ...options })
const el = ref.current
if (el) observer.observe(el)
return () => { if (el) observer.unobserve(el) }
}, [])
return [ref, isVisible]
}
Pay attention to boundary condition handling, which is critical in production.
Summary
- React 18 Suspense SSR 流式渲染不是银弹,需要根据项目规模和技术栈选择
- Understanding underlying principles is more important than memorizing APIs
- Always verify compatibility before using in production
- In team collaboration, conventions and documentation are more important than the technology itself
- Stay updated with the community; technical solutions need continuous iteration