当前位置: 首页 > news >正文

沧州建设网站的公司网站制作 合肥

沧州建设网站的公司,网站制作 合肥,sem推广和seo的区别,亚洲最新永久在线观看Context 提供了一个无需为每层组件手动添加 props ,就能在组件树间进行数据传递的方法,useContext 用于函数组件中订阅上层 context 的变更,可以获取上层 context 传递的 value prop 值 useContext 接收一个 context 对象(React.…

Context 提供了一个无需为每层组件手动添加 props ,就能在组件树间进行数据传递的方法,useContext 用于函数组件中订阅上层 context 的变更,可以获取上层 context 传递的 value prop 值

useContext 接收一个 context 对象(React.createContext的返回值)并返回 context 的当前值,当前的 context 值由上层组件中距离当前组件最近的 <MyContext.Provider>value prop 决定

const value = useContext(MyContext);

 使用:

import React, { useContext, useState } from 'react';const themes = {light: {foreground: "#000000",background: "#eeeeee"},dark: {foreground: "#ffffff",background: "#222222"}
};// 为当前 theme 创建一个 context
const ThemeContext = React.createContext();export default function Toolbar(props) {const [theme, setTheme] = useState(themes.dark);const toggleTheme = () => {setTheme(currentTheme => (currentTheme === themes.dark? themes.light: themes.dark));};return (// 使用 Provider 将当前 props.value 传递给内部组件<ThemeContext.Provider value={{theme, toggleTheme}}><ThemeButton /></ThemeContext.Provider>);
}function ThemeButton() {// 通过 useContext 获取当前 context 值const { theme, toggleTheme } = useContext(ThemeContext);return (<button style={{background: theme.background, color: theme.foreground }} onClick={toggleTheme}>Change the button's theme</button>);
}

 等价 class的示例,如下:

useContext(MyContext) 相当于 class 组件中的 static contextType = MyContext 或者 <MyContext.Consumer>

useContext 并没有改变消费 context 的方式,它只为我们提供了一种额外的、更漂亮的、更漂亮的方法来消费上层 context。在将其应用于使用多 context 的组件时将会非常有用

import React from 'react';const themes = {light: {foreground: "#000000",background: "#eeeeee"},dark: {foreground: "#ffffff",background: "#222222"}
};const ThemeContext = React.createContext(themes.light);function ThemeButton() {return (<ThemeContext.Consumer>{({theme, toggleTheme}) => (<button style={{background: theme.background, color: theme.foreground }} onClick={toggleTheme}>Change the button's theme</button>)}</ThemeContext.Consumer>);
}export default class Toolbar extends React.Component {constructor(props) {super(props);this.state = {theme: themes.light};this.toggleTheme = this.toggleTheme.bind(this);}toggleTheme() {this.setState(state => ({theme:state.theme === themes.dark? themes.light: themes.dark}));}render() {return (<ThemeContext.Provider value={{ theme: this.state.theme, toggleTheme: this.toggleTheme }}><ThemeButton /></ThemeContext.Provider>)}
}

 优化消费 context 组件:

调用了 useContext 的组件都会在 context 值变化时重新渲染,为了减少重新渲染组件的较大开销,可以通过使用 memoization 来优化

假设由于某种原因,您有 AppContext,其值具有 theme 属性,并且您只想在 appContextValue.theme 更改上重新渲染一些 ExpensiveTree

  1. 方式1: 拆分不会一起更改的 context
  2. 当不能拆分 context 时,将组件一分为二,给中间组件加上 React.memo
  3. 返回一个内置 useMemo 的组件
function Button() {// 把 theme context 拆分出来,其他 context 变化时不会导致 ExpensiveTree 重新渲染let theme = useContext(ThemeContext);return <ExpensiveTree className={theme} />;
}
function Button() {let appContextValue = useContext(AppContext);let theme = appContextValue.theme; // 获取 theme 属性return <ThemedButton theme={theme} />
}const ThemedButton = memo(({ theme }) => {// 使用 memo 尽量复用上一次渲染结果return <ExpensiveTree className={theme} />;
});
function Button() {let appContextValue = useContext(AppContext);let theme = appContextValue.theme; // 获取 theme 属性return useMemo(() => {// The rest of your rendering logicreturn <ExpensiveTree className={theme} />;}, [theme])
}
http://www.yayakq.cn/news/853912/

相关文章:

  • 网站如何做手机端页面apk连接wordpress
  • 网站发帖百度收录动易做网站如何
  • 深圳建站模板公司企业网站模板下载选哪家
  • 凡科快速建站免费企业邮箱哪家好
  • 如何利用网站模板wordpress 模板带数据
  • 网站空间 默认文档邢台市人口
  • 网站seo顾问电子商务网站建设组织流程图
  • 网站模板建设表白网页在线生成器
  • 怎么做网站视频教程凡科免费个人做网站有弊吗
  • 机械行业网站建设方案微信小程序开发工具教程
  • 网站算信息化建设东莞网站建设 硅胶
  • 网站做跳转的意义基于html5的旅游网站开发
  • 微信小程序商城怎么弄济南优化网站厂家
  • 猎头网站模板网站建设合同技术开发合同
  • 如何优化公司网站汽车设计公司排名前十强
  • 营销型网站试运营调忧app下载中心
  • 做网站横幅技巧杏坛网站建设
  • 物流网站建设的小结wordpress页面的排序
  • 苏州做网站套路骗建站哪家公司比较好而且不贵
  • 药品加工厂做网站三合一做网站
  • 成都网站快照优化公司苏州建站公司认准苏州聚尚网络
  • 无锡网站制作哪里有小型工作室项目大全
  • 建设一个旅游网站厦门seo全网营销
  • 织梦商城网站源码盘锦威旺做网站建设公司
  • 跟犀牛云一样做网站的html网站开发图片素材
  • 淮阴区住房和城乡建设局网站电商主图一键生成免费
  • 营销型网站架构师seo内容优化是什么
  • 用织梦做外文网站怎么得到wordpress文章加图片
  • h5网站有哪些wordpress是国外的吗
  • 兰州网站建设net网站开发 兼职