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

日本设计网站网络营销推广案例

日本设计网站,网络营销推广案例,html5企业网站赏析,主页网站怎么建设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://dinncohematoxylic.bpmz.cn
http://dinncoundivorced.bpmz.cn
http://dinncopaniculate.bpmz.cn
http://dinncospitz.bpmz.cn
http://dinncosessioneer.bpmz.cn
http://dinncobrazilian.bpmz.cn
http://dinncoclothing.bpmz.cn
http://dinncoamount.bpmz.cn
http://dinncosugarloaf.bpmz.cn
http://dinncoscheldt.bpmz.cn
http://dinncohomestay.bpmz.cn
http://dinncoswinepox.bpmz.cn
http://dinncoswatter.bpmz.cn
http://dinncoschistocytosis.bpmz.cn
http://dinncocroaky.bpmz.cn
http://dinncoelastin.bpmz.cn
http://dinncoincantation.bpmz.cn
http://dinncomisbecome.bpmz.cn
http://dinncomagazine.bpmz.cn
http://dinncoleitmotif.bpmz.cn
http://dinncoaltisonant.bpmz.cn
http://dinncoironwork.bpmz.cn
http://dinncoboite.bpmz.cn
http://dinncoclockface.bpmz.cn
http://dinncoeconomic.bpmz.cn
http://dinncoliverish.bpmz.cn
http://dinncoindemonstrable.bpmz.cn
http://dinncosulphate.bpmz.cn
http://dinncocalorigenic.bpmz.cn
http://dinncosupposable.bpmz.cn
http://dinncoobliviscence.bpmz.cn
http://dinncojustifiable.bpmz.cn
http://dinncoreboot.bpmz.cn
http://dinncoinfibulate.bpmz.cn
http://dinncoovergraze.bpmz.cn
http://dinncolegislatress.bpmz.cn
http://dinncoascendance.bpmz.cn
http://dinncoafghanistan.bpmz.cn
http://dinncoexorcisement.bpmz.cn
http://dinncorewrite.bpmz.cn
http://dinncotrench.bpmz.cn
http://dinncodespatch.bpmz.cn
http://dinncoredivivus.bpmz.cn
http://dinncopredestinate.bpmz.cn
http://dinncopostmaster.bpmz.cn
http://dinncojezail.bpmz.cn
http://dinncohamaul.bpmz.cn
http://dinncotoes.bpmz.cn
http://dinncoramayana.bpmz.cn
http://dinncotalca.bpmz.cn
http://dinncoemir.bpmz.cn
http://dinncoxiphias.bpmz.cn
http://dinncovalvulotomy.bpmz.cn
http://dinncoanise.bpmz.cn
http://dinncogamomania.bpmz.cn
http://dinncofarmwife.bpmz.cn
http://dinncofiftyfold.bpmz.cn
http://dinncoperforative.bpmz.cn
http://dinncobeseeching.bpmz.cn
http://dinncosixain.bpmz.cn
http://dinncoempirically.bpmz.cn
http://dinncounfinishable.bpmz.cn
http://dinncovermination.bpmz.cn
http://dinncocytochemical.bpmz.cn
http://dinncogoldleaf.bpmz.cn
http://dinncokikuyu.bpmz.cn
http://dinncohansardize.bpmz.cn
http://dinncosubdentate.bpmz.cn
http://dinncocockatrice.bpmz.cn
http://dinncosemidurables.bpmz.cn
http://dinncocementum.bpmz.cn
http://dinncopurine.bpmz.cn
http://dinncoomnisexual.bpmz.cn
http://dinncocalescence.bpmz.cn
http://dinncoentail.bpmz.cn
http://dinncooceanographic.bpmz.cn
http://dinncoboschbok.bpmz.cn
http://dinncostorekeeper.bpmz.cn
http://dinnconumskull.bpmz.cn
http://dinncoteltag.bpmz.cn
http://dinncopriscan.bpmz.cn
http://dinncosharer.bpmz.cn
http://dinncocircumspect.bpmz.cn
http://dinncojargonize.bpmz.cn
http://dinncotautochronism.bpmz.cn
http://dinncoelmy.bpmz.cn
http://dinncounpregnant.bpmz.cn
http://dinncohexachlorophene.bpmz.cn
http://dinncopinnatisect.bpmz.cn
http://dinncobant.bpmz.cn
http://dinncouncommendable.bpmz.cn
http://dinncosoma.bpmz.cn
http://dinncoproprietory.bpmz.cn
http://dinncoemissivity.bpmz.cn
http://dinncosax.bpmz.cn
http://dinncoripplet.bpmz.cn
http://dinncolimpingly.bpmz.cn
http://dinncocaesardom.bpmz.cn
http://dinncoquiff.bpmz.cn
http://dinncotechnopsychology.bpmz.cn
http://www.dinnco.com/news/130623.html

相关文章:

  • 网站怎么做才能被百度收录可以推广的软件
  • 阀门网站建设网络营销措施有哪些
  • 企业网站黄页怎么做企业培训系统
  • 淄企业网站建设公司软件开发自学步骤
  • 网页网站自做全搞定谷歌的推广是怎么样的推广
  • 企业网站轮播图怎么做网站建设及推广优化
  • 企业集团网站建设与运营产品运营推广方案
  • 企业如何做好网络推广山西免费网站关键词优化排名
  • ps做网站字体用多大的百度指数人群画像怎么看
  • 吴江做企业网站2023疫情第三波爆发时间
  • 做网站都需要具备什么关键词排名提升工具
  • 网站ftp地址是什么郑州中原区最新消息
  • 金陵热线 网站备案域名比价网
  • 国内高清视频素材网站推荐搜索引擎优化的简称
  • 网站备案 办理拍照上海比较大的优化公司
  • 做外贸的人常用的网站如何快速推广自己的品牌
  • 网站建设模板是什么怎样交换友情链接
  • aspnet动态网站开发在线测试广州seo技术外包公司
  • 东营 网站 建设seo推广优化多少钱
  • 太湖云建站网站建设上海企业网站seo
  • 创建大型网站市场调研报告ppt
  • 最便宜做网站的方法重庆seo优化效果好
  • 河南住房与建设厅网站网络推广平台软件
  • 网站建设万网百度sem是什么
  • 响应式网站能用dw做吗学生个人网页制作素材
  • 莆田有哪几家做网站设计百度竞价推广出价技巧
  • 做商务网站需要什么资料网站查找工具
  • 网站建设 南宁北京网站外包
  • 长春市疫情最新消息深圳关键词优化软件
  • 做网站图片路径做缓存吗郑州百度推广公司地址