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

怎么搜才能搜到网站东莞网站到首页排名

怎么搜才能搜到网站,东莞网站到首页排名,html个人网站完整代码,网站开发可行性分析报告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.tj-hxxt.cn/news/121575.html

相关文章:

  • 福州市网站建设情感营销
  • 北京大学两学一做网站千牛怎么做免费推广引流
  • 产品网站建设框架网络营销策划的概念
  • 同学聚会怎么样做网站网站搭建关键词排名
  • 网站制作建设公司测试自己适不适合做销售
  • 成都网站设计创新互联长沙网站seo优化公司
  • 建设个网站从哪里盈利百度快照没有了用什么代替了
  • 网站建设实例pdf下载趣丁号友情链接
  • erp软件有哪些品牌天天seo伪原创工具
  • 做网站横幅的软件抖音seo代理
  • 微信公众号接口文档seo视频教程
  • 商城网站管理系统seo推广软件品牌
  • wordpress 网站建设网址查询入口
  • 销售一个产品的网站怎么做的武汉seo网站优化运营
  • 做网站前期创建文件夹品牌营销是什么
  • 东莞广告公司东莞网站建设平台推广网站
  • 快速做网站视频油烟机seo关键词
  • 国外做电子元器件在哪个网站庆云网站seo
  • 哪个网站做自媒体比较好长沙网站优化效果
  • 黑龙江省住房和城乡建设网站推广营销平台
  • 一般网站用什么软件做青岛seo代理计费
  • 长春市长春网站制作站优化百度seo软件
  • 南宁做网站推广基本营销策略有哪些
  • 张店网站制作代做百度首页排名
  • 网站功能报价黄页网推广服务
  • 台州网页设计招聘信息网页优化最为重要的内容是
  • 国外网站建立百度网站推广
  • 有没有免费做任务赚钱的网站优化防控举措
  • 什么网站做任务可以赚钱aso具体优化
  • 个人网站做团购安徽网站设计