文档

文档

React

LazyMotion

LazyMotion

为了易用性,motion组件预先捆绑了所有功能,bundle 体积约为 34kb。

使用 LazyMotionm 组件,我们可以将初始渲染的体积减小到 6kb,然后同步或异步加载部分功能。

import { LazyMotion, domAnimations } from "motion/react"
import * as m from "motion/react-m"

export const MyComponent = ({ isVisible }) => (
  <LazyMotion features={domAnimations}>
    <m.div animate={{ opacity: 1 }} />
  </LazyMotion>
)

阅读减小 bundle 体积指南以获取完整的使用说明。

Props

features

定义要同步或异步加载的功能包。

同步加载

同步加载适用于为较小的 bundle 体积定义部分功能。

import { LazyMotion, domAnimations } from "motion/react"
import * as m from "motion/react-m"

export const MyComponent = ({ isVisible }) => (
  <LazyMotion features={domAnimations}>
    <m.div animate={{ opacity: 1 }} />
  </LazyMotion>
)

异步加载

异步加载可以确保您的网站在加载部分或全部动画功能之前完成 hydration。

// features.js
import { domAnimations } from "motion/react"
export default domAnimations
  
// index.js
const loadFeatures = import("./features.js")
  .then(res => res.default)

function Component() {
  return (
    <LazyMotion features={loadFeatures}>
      <m.div animate={{ scale: 1.5 }} />
    </LazyMotion>
  )
}

strict

默认值: false

如果 true,则当 motion 组件在 LazyMotion 组件内渲染时(从而消除懒加载的 bundle 体积优势),将抛出一个错误。

// This component will throw an error that explains using a motion component
// instead of the m component will break the benefits of code-splitting.
function Component() {
  return (
    <LazyMotion features={domAnimations} strict>
      <motion.div />
    </LazyMotion>
  )
}

为了易用性,motion组件预先捆绑了所有功能,bundle 体积约为 34kb。

使用 LazyMotionm 组件,我们可以将初始渲染的体积减小到 6kb,然后同步或异步加载部分功能。

import { LazyMotion, domAnimations } from "motion/react"
import * as m from "motion/react-m"

export const MyComponent = ({ isVisible }) => (
  <LazyMotion features={domAnimations}>
    <m.div animate={{ opacity: 1 }} />
  </LazyMotion>
)

阅读减小 bundle 体积指南以获取完整的使用说明。

Props

features

定义要同步或异步加载的功能包。

同步加载

同步加载适用于为较小的 bundle 体积定义部分功能。

import { LazyMotion, domAnimations } from "motion/react"
import * as m from "motion/react-m"

export const MyComponent = ({ isVisible }) => (
  <LazyMotion features={domAnimations}>
    <m.div animate={{ opacity: 1 }} />
  </LazyMotion>
)

异步加载

异步加载可以确保您的网站在加载部分或全部动画功能之前完成 hydration。

// features.js
import { domAnimations } from "motion/react"
export default domAnimations
  
// index.js
const loadFeatures = import("./features.js")
  .then(res => res.default)

function Component() {
  return (
    <LazyMotion features={loadFeatures}>
      <m.div animate={{ scale: 1.5 }} />
    </LazyMotion>
  )
}

strict

默认值: false

如果 true,则当 motion 组件在 LazyMotion 组件内渲染时(从而消除懒加载的 bundle 体积优势),将抛出一个错误。

// This component will throw an error that explains using a motion component
// instead of the m component will break the benefits of code-splitting.
function Component() {
  return (
    <LazyMotion features={domAnimations} strict>
      <motion.div />
    </LazyMotion>
  )
}

为了易用性,motion组件预先捆绑了所有功能,bundle 体积约为 34kb。

使用 LazyMotionm 组件,我们可以将初始渲染的体积减小到 6kb,然后同步或异步加载部分功能。

import { LazyMotion, domAnimations } from "motion/react"
import * as m from "motion/react-m"

export const MyComponent = ({ isVisible }) => (
  <LazyMotion features={domAnimations}>
    <m.div animate={{ opacity: 1 }} />
  </LazyMotion>
)

阅读减小 bundle 体积指南以获取完整的使用说明。

Props

features

定义要同步或异步加载的功能包。

同步加载

同步加载适用于为较小的 bundle 体积定义部分功能。

import { LazyMotion, domAnimations } from "motion/react"
import * as m from "motion/react-m"

export const MyComponent = ({ isVisible }) => (
  <LazyMotion features={domAnimations}>
    <m.div animate={{ opacity: 1 }} />
  </LazyMotion>
)

异步加载

异步加载可以确保您的网站在加载部分或全部动画功能之前完成 hydration。

// features.js
import { domAnimations } from "motion/react"
export default domAnimations
  
// index.js
const loadFeatures = import("./features.js")
  .then(res => res.default)

function Component() {
  return (
    <LazyMotion features={loadFeatures}>
      <m.div animate={{ scale: 1.5 }} />
    </LazyMotion>
  )
}

strict

默认值: false

如果 true,则当 motion 组件在 LazyMotion 组件内渲染时(从而消除懒加载的 bundle 体积优势),将抛出一个错误。

// This component will throw an error that explains using a motion component
// instead of the m component will break the benefits of code-splitting.
function Component() {
  return (
    <LazyMotion features={domAnimations} strict>
      <motion.div />
    </LazyMotion>
  )
}
保持关注

订阅以获取最新消息和更新。

保持关注

订阅以获取最新消息和更新。