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

中山外贸网站建设公司吉林关键词排名优化软件

中山外贸网站建设公司,吉林关键词排名优化软件,2017设计工作室做网站,哪个平台免费招人最快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.15wanjia.com/news/28469.html

相关文章:

  • 黄冈网站推广软件哪里买站外推广怎么做
  • wordpress+一页一屏优化seo排名
  • 做网站要收订金吗百度信息流广告代理
  • 做自媒体一般都注册几个网站百度风云排行榜
  • 优化是企业通过网站来做吗推广目标怎么写
  • 淘宝客必须做网站优化教程网下载
  • 怎么做微信小说网站免费推广网站大全下载
  • 辽宁建设工程信息网业绩录入windows10优化大师
  • html5移动网站制作教程北京seo做排名
  • 专门做招商的网站是什么情况搜狐新闻手机网
  • 有没有做链接的网站链接优化方法
  • seo问答关键词优化公司哪家推广
  • 服装网页设计模板seo谷歌
  • 开发app找什么公司百度关键词在线优化
  • 宝塔面板怎么做网站人力资源培训机构
  • 旅游网站模板源码建个网站费用多少
  • 企业顺德网站建设长尾词挖掘工具
  • 有经验的南昌网站建设成都网站优化seo
  • 东莞网站建设_东莞网页设计亚马逊查关键词排名工具
  • wordpress魔改优化大师在哪里
  • 图片在线制作表情包沈阳seo推广
  • 用jsp做网站有什么好处长治seo顾问
  • 如何网站域名到其他公司如何进行网络推广
  • 河南省建设工程网站谷歌seo查询
  • 网站主机价格西安网站托管
  • 湛江做网站做专业搜索引擎优化
  • 网站推广有什么方法有哪些什么网站可以免费推广
  • 新疆建设工程信息网站北京seo排名公司
  • 做网站怎么买服务器吗百度秒收录
  • 沧州市东光建设局 网站网络推广渠道公司