前言
Context
被翻譯為上下文,在編程領(lǐng)域,這是一個(gè)經(jīng)常會(huì)接觸到的概念,React中也有。
在React的官方文檔中,Context
被歸類為高級(jí)部分(Advanced),屬于React的高級(jí)API,但官方并不建議在穩(wěn)定版的App中使用Context。
The vast majority of applications do not need to use content.
If you want your application to be stable, don't use context. It is an experimental API and it is likely to break in future releases of React.
不過(guò),這并非意味著我們不需要關(guān)注Context
。事實(shí)上,很多優(yōu)秀的React組件都通過(guò)Context來(lái)完成自己的功能,比如react-redux的<Provider />
,就是通過(guò)Context
提供一個(gè)全局態(tài)的store
,拖拽組件react-dnd,通過(guò)Context
在組件中分發(fā)DOM的Drag和Drop事件,路由組件react-router通過(guò)Context
管理路由狀態(tài)等等。在React組件開發(fā)中,如果用好Context
,可以讓你的組件變得強(qiáng)大,而且靈活。
今天就想跟大家聊一聊,我在開發(fā)當(dāng)中,所認(rèn)識(shí)到的這個(gè)Context
,以及我是如何使用它來(lái)進(jìn)行組件開發(fā)的。
注:本文中所有提到的App皆指Web端App。
初識(shí)React Context
官方對(duì)于Context的定義
React文檔官網(wǎng)并未對(duì)Context
給出“是什么”的定義,更多是描述使用的Context
的場(chǎng)景,以及如何使用Context
。
官網(wǎng)對(duì)于使用Context
的場(chǎng)景是這樣描述的:
In Some Cases, you want to pass data through the component tree without having to pass the props down manuallys at every level. you can do this directly in React with the powerful "context" API.
簡(jiǎn)單說(shuō)就是,當(dāng)你不想在組件樹中通過(guò)逐層傳遞props
或者state
的方式來(lái)傳遞數(shù)據(jù)時(shí),可以使用Context
來(lái)實(shí)現(xiàn)跨層級(jí)的組件數(shù)據(jù)傳遞。
使用props或者state傳遞數(shù)據(jù),數(shù)據(jù)自頂下流。
使用Context
,可以跨越組件進(jìn)行數(shù)據(jù)傳遞。
如何使用Context
如果要Context
發(fā)揮作用,需要用到兩種組件,一個(gè)是Context
生產(chǎn)者(Provider),通常是一個(gè)父節(jié)點(diǎn),另外是一個(gè)Context
的消費(fèi)者(Consumer),通常是一個(gè)或者多個(gè)子節(jié)點(diǎn)。所以Context
的使用基于生產(chǎn)者消費(fèi)者模式。
對(duì)于父組件,也就是Context
生產(chǎn)者,需要通過(guò)一個(gè)靜態(tài)屬性childContextTypes
聲明提供給子組件的Context
對(duì)象的屬性,并實(shí)現(xiàn)一個(gè)實(shí)例getChildContext
方法,返回一個(gè)代表Context
的純對(duì)象 (plain object) 。
import React from 'react'
import PropTypes from 'prop-types'
class MiddleComponent extends React.Component {
render () {
return <ChildComponent />
}
}
class ParentComponent extends React.Component {
// 聲明Context對(duì)象屬性
static childContextTypes = {
propA: PropTypes.string,
methodA: PropTypes.func
}
// 返回Context對(duì)象,方法名是約定好的
getChildContext () {
return {
propA: 'propA',
methodA: () => 'methodA'
}
}
render () {
return <MiddleComponent />
}
}
而對(duì)于Context
的消費(fèi)者,通過(guò)如下方式訪問(wèn)父組件提供的Context
。
import React from 'react'
import PropTypes from 'prop-types'
class ChildComponent extends React.Component {
// 聲明需要使用的Context屬性
static contextTypes = {
propA: PropTypes.string
}
render () {
const {
propA,
methodA
} = this.context
console.log(`context.propA = ${propA}`) // context.propA = propA
console.log(`context.methodA = ${methodA}`) // context.methodA = undefined
return ...
}
}
子組件需要通過(guò)一個(gè)靜態(tài)屬性contextTypes
聲明后,才能訪問(wèn)父組件Context
對(duì)象的屬性,否則,即使屬性名沒(méi)寫錯(cuò),拿到的對(duì)象也是undefined
。
對(duì)于無(wú)狀態(tài)子組件(Stateless Component),可以通過(guò)如下方式訪問(wèn)父組件的Context
import React from 'react'
import PropTypes from 'prop-types'
const ChildComponent = (props, context) => {
const {
propA
} = context
console.log(`context.propA = ${propA}`) // context.propA = propA
return ...
}
ChildComponent.contextProps = {
propA: PropTypes.string
}
而在接下來(lái)的發(fā)行版本中,React對(duì)Context
的API做了調(diào)整,更加明確了生產(chǎn)者消費(fèi)者模式的使用方式。
import React from 'react';
import ReactDOM from 'react-dom';
const ThemeContext = React.createContext({
background: 'red',
color: 'white'
});
通過(guò)靜態(tài)方法React.createContext()
創(chuàng)建一個(gè)Context
對(duì)象,這個(gè)Context
對(duì)象包含兩個(gè)組件,<Provider />
和<Consumer />
。
class App extends React.Component {
render () {
return (
<ThemeContext.Provider value={{background: 'green', color: 'white'}}>
<Header />
</ThemeContext.Provider>
);
}
}
<Provider />
的value
相當(dāng)于現(xiàn)在的getChildContext()
。
class Header extends React.Component {
render () {
return (
<Title>Hello React Context API</Title>
);
}
}
class Title extends React.Component {
render () {
return (
<ThemeContext.Consumer>
{context => (
<h1 style={{background: context.background, color: context.color}}>
{this.props.children}
</h1>
)}
</ThemeContext.Consumer>
);
}
}
<Consumer />
的children
必須是一個(gè)函數(shù),通過(guò)函數(shù)的參數(shù)獲取<Provider />
提供的Context
。
可見,Context
的新API更加貼近React的風(fēng)格。
幾個(gè)可以直接獲取Context的地方
實(shí)際上,除了實(shí)例的context
屬性(this.context
),React組件還有很多個(gè)地方可以直接訪問(wèn)父組件提供的Context
。比如構(gòu)造方法:
constructor(props, context)
比如生命周期:
componentWillReceiveProps(nextProps, nextContext)
shouldComponentUpdate(nextProps, nextState, nextContext)
componetWillUpdate(nextProps, nextState, nextContext)
對(duì)于面向函數(shù)的無(wú)狀態(tài)組件,可以通過(guò)函數(shù)的參數(shù)直接訪問(wèn)組件的Context
。
const StatelessComponent = (props, context) => (
......
)
以上是Context
的基礎(chǔ),更具體的指南內(nèi)容可參見這里
我對(duì)Context的理解
OK,說(shuō)完基礎(chǔ)的東西,現(xiàn)在聊一聊我對(duì)React的Context
的理解。
把Context當(dāng)做組件作用域
使用React的開發(fā)者都知道,一個(gè)React App本質(zhì)就是一棵React組件樹,每個(gè)React組件相當(dāng)于這棵樹上的一個(gè)節(jié)點(diǎn),除了App的根節(jié)點(diǎn),其他每個(gè)節(jié)點(diǎn)都存在一條父組件鏈。
例如上圖,<Child />
的父組件鏈?zhǔn)?code><SubNode /> -- <Node />
-- <App />
,<SubNode />
的父組件鏈?zhǔn)?code><Node /> -- <App />
,<Node />
的父組件鏈只有一個(gè)組件節(jié)點(diǎn),就是<App />
。
這些以樹狀連接的組件節(jié)點(diǎn),實(shí)際上也組成了一棵Context
樹,每個(gè)節(jié)點(diǎn)的Context
,來(lái)自父組件鏈上所有組件節(jié)點(diǎn)通過(guò)getChildContext()
所提供的Context
對(duì)象組合而成的對(duì)象。
有了解JS作用域鏈概念的開發(fā)者應(yīng)該都知道,JS的代碼塊在執(zhí)行期間,會(huì)創(chuàng)建一個(gè)相應(yīng)的作用域鏈,這個(gè)作用域鏈記錄著運(yùn)行時(shí)JS代碼塊執(zhí)行期間所能訪問(wèn)的活動(dòng)對(duì)象,包括變量和函數(shù),JS程序通過(guò)作用域鏈訪問(wèn)到代碼塊內(nèi)部或者外部的變量和函數(shù)。
假如以JS的作用域鏈作為類比,React組件提供的Context
對(duì)象其實(shí)就好比一個(gè)提供給子組件訪問(wèn)的作用域,而Context
對(duì)象的屬性可以看成作用域上的活動(dòng)對(duì)象。由于組件的Context
由其父節(jié)點(diǎn)鏈上所有組件通過(guò)getChildContext()
返回的Context
對(duì)象組合而成,所以,組件通過(guò)Context
是可以訪問(wèn)到其父組件鏈上所有節(jié)點(diǎn)組件提供的Context
的屬性。
所以,我借鑒了JS作用域鏈的思路,把Context
當(dāng)成是組件的作用域來(lái)使用。
關(guān)注Context的可控性和影響范圍
不過(guò),作為組件作用域來(lái)看待的Context
與常見的作用域的概念 (就我個(gè)人目前接觸到的編程語(yǔ)言而言) 是有所區(qū)別的。我們需要關(guān)注Context
的可控性和影響范圍。
在我們平時(shí)的開發(fā)中,用到作用域或者上下文的場(chǎng)景是很常見,很自然,甚至是無(wú)感知的,然而,在React中使用Context
并不是那么容易。父組件提供Context
需要通過(guò)childContextTypes
進(jìn)行“聲明”,子組件使用父組件的Context
屬性需要通過(guò)contextTypes
進(jìn)行“申請(qǐng)”,所以,我認(rèn)為React的Context
是一種“帶權(quán)限”的組件作用域。
這種“帶權(quán)限”的方式有何好處?就我個(gè)人的理解,首先是保持框架API的一致性,和propTypes
一樣,使用聲明式編碼風(fēng)格。另外就是,可以在一定程度上確保組件所提供的Context
的可控性和影響范圍。
React App的組件是樹狀結(jié)構(gòu),一層一層延伸,父子組件是一對(duì)多的線性依賴。隨意的使用Context
其實(shí)會(huì)破壞這種依賴關(guān)系,導(dǎo)致組件之間一些不必要的額外依賴,降低組件的復(fù)用性,進(jìn)而可能會(huì)影響到App的可維護(hù)性。
通過(guò)上圖可以看到,原本線性依賴的組件樹,由于子組件使用了父組件的Context
,導(dǎo)致<Child />
組件對(duì)<Node />
和<App />
都產(chǎn)生了依賴關(guān)系。一旦脫離了這兩個(gè)組件,<Child />
的可用性就無(wú)法保障了,減低了<Child />
的復(fù)用性。
在我看來(lái),通過(guò)Context
暴露數(shù)據(jù)或者API不是一種優(yōu)雅的實(shí)踐方案,盡管react-redux是這么干的。因此需要一種機(jī)制,或者說(shuō)約束,去降低不必要的影響。
通過(guò)childContextTypes
和contextTypes
這兩個(gè)靜態(tài)屬性的約束,可以在一定程度保障,只有組件自身,或者是與組件相關(guān)的其他子組件才可以隨心所欲的訪問(wèn)Context
的屬性,無(wú)論是數(shù)據(jù)還是函數(shù)。因?yàn)橹挥薪M件自身或者相關(guān)的子組件可以清楚它能訪問(wèn)Context
哪些屬性,而相對(duì)于那些與組件無(wú)關(guān)的其他組件,無(wú)論是內(nèi)部或者外部的 ,由于不清楚父組件鏈上各父組件的childContextTypes
“聲明”了哪些Context
屬性,所以沒(méi)法通過(guò)contextTypes
“申請(qǐng)”相關(guān)的屬性。所以我理解為,給組件的作用域Context
“帶權(quán)限”,可以在一定程度上確保Context
的可控性和影響范圍。
在開發(fā)組件過(guò)程中,我們應(yīng)該時(shí)刻關(guān)注這一點(diǎn),不要隨意的使用Context
。
不需要優(yōu)先使用Context
作為React的高級(jí)API,React并不推薦我們優(yōu)先考慮使用Context
。我的理解是:
-
Context
目前還處于實(shí)驗(yàn)階段,可能會(huì)在后面的發(fā)行版本中有大的變化,事實(shí)上這種情況已經(jīng)發(fā)生了,所以為了避免給今后升級(jí)帶來(lái)較大影響和麻煩,不建議在App中使用Context
。 - 盡管不建議在App中使用
Context
,但對(duì)于組件而言,由于影響范圍小于App,如果可以做到高內(nèi)聚,不破壞組件樹的依賴關(guān)系,那么還是可以考慮使用Context
的。 - 對(duì)于組件之間的數(shù)據(jù)通信或者狀態(tài)管理,優(yōu)先考慮用
props
或者state
解決,然后再考慮用其他第三方成熟庫(kù)解決的,以上方法都不是最佳選擇的時(shí)候,那么再考慮使用Context
。 -
Context
的更新需要通過(guò)setState()
觸發(fā),但是這并不是可靠的。Context
支持跨組件訪問(wèn),但是,如果中間的子組件通過(guò)一些方法不響應(yīng)更新,比如shouldComponentUpdate()
返回false
,那么不能保證Context
的更新一定可達(dá)使用Context
的子組件。因此,Context
的可靠性需要關(guān)注。不過(guò)更新的問(wèn)題,在新版的API中得以解決。
簡(jiǎn)而言之,只要你能確保Context
是可控的,使用Context
并無(wú)大礙,甚至如果能夠合理的應(yīng)用,Context
其實(shí)可以給React組件開發(fā)帶來(lái)很強(qiáng)大的體驗(yàn)。
用Context作為共享數(shù)據(jù)的媒介
官方所提到Context
可以用來(lái)進(jìn)行跨組件的數(shù)據(jù)通信。而我,把它理解為,好比一座橋,作為一種作為媒介進(jìn)行數(shù)據(jù)共享。數(shù)據(jù)共享可以分兩類:App級(jí)與組件級(jí)。
- App級(jí)的數(shù)據(jù)共享
App根節(jié)點(diǎn)組件提供的Context
對(duì)象可以看成是App級(jí)的全局作用域,所以,我們利用App根節(jié)點(diǎn)組件提供的Context
對(duì)象創(chuàng)建一些App級(jí)的全局?jǐn)?shù)據(jù)。現(xiàn)成的例子可以參考react-redux,以下是<Provider />
組件源碼的核心實(shí)現(xiàn):
export function createProvider(storeKey = 'store', subKey) {
const subscriptionKey = subKey || `${storeKey}Subscription`
class Provider extends Component {
getChildContext() {
return { [storeKey]: this[storeKey], [subscriptionKey]: null }
}
constructor(props, context) {
super(props, context)
this[storeKey] = props.store;
}
render() {
return Children.only(this.props.children)
}
}
// ......
Provider.propTypes = {
store: storeShape.isRequired,
children: PropTypes.element.isRequired,
}
Provider.childContextTypes = {
[storeKey]: storeShape.isRequired,
[subscriptionKey]: subscriptionShape,
}
return Provider
}
export default createProvider()
App的根組件用<Provider />
組件包裹后,本質(zhì)上就為App提供了一個(gè)全局的屬性store
,相當(dāng)于在整個(gè)App范圍內(nèi),共享store
屬性。當(dāng)然,<Provider />
組件也可以包裹在其他組件中,在組件級(jí)的全局范圍內(nèi)共享store
。
- 組件級(jí)的數(shù)據(jù)共享
如果組件的功能不能單靠組件自身來(lái)完成,還需要依賴額外的子組件,那么可以利用Context
構(gòu)建一個(gè)由多個(gè)子組件組合的組件。例如,react-router。
react-router的<Router />
自身并不能獨(dú)立完成路由的操作和管理,因?yàn)閷?dǎo)航鏈接和跳轉(zhuǎn)的內(nèi)容通常是分離的,因此還需要依賴<Link />
和<Route />
等子組件來(lái)一同完成路由的相關(guān)工作。為了讓相關(guān)的子組件一同發(fā)揮作用,react-router的實(shí)現(xiàn)方案是利用Context
在<Router />
、<Link />
以及<Route />
這些相關(guān)的組件之間共享一個(gè)router
,進(jìn)而完成路由的統(tǒng)一操作和管理。
下面截取<Router />
、<Link />
以及<Route />
這些相關(guān)的組件部分源碼,以便更好的理解上述所說(shuō)的。
// Router.js
/**
* The public API for putting history on context.
*/
class Router extends React.Component {
static propTypes = {
history: PropTypes.object.isRequired,
children: PropTypes.node
};
static contextTypes = {
router: PropTypes.object
};
static childContextTypes = {
router: PropTypes.object.isRequired
};
getChildContext() {
return {
router: {
...this.context.router,
history: this.props.history,
route: {
location: this.props.history.location,
match: this.state.match
}
}
};
}
// ......
componentWillMount() {
const { children, history } = this.props;
// ......
this.unlisten = history.listen(() => {
this.setState({
match: this.computeMatch(history.location.pathname)
});
});
}
// ......
}
盡管源碼還有其他的邏輯,但<Router />
的核心就是為子組件提供一個(gè)帶有router
屬性的Context
,同時(shí)監(jiān)聽history
,一旦history
發(fā)生變化,便通過(guò)setState()
觸發(fā)組件重新渲染。
// Link.js
/**
* The public API for rendering a history-aware <a>.
*/
class Link extends React.Component {
// ......
static contextTypes = {
router: PropTypes.shape({
history: PropTypes.shape({
push: PropTypes.func.isRequired,
replace: PropTypes.func.isRequired,
createHref: PropTypes.func.isRequired
}).isRequired
}).isRequired
};
handleClick = event => {
if (this.props.onClick) this.props.onClick(event);
if (
!event.defaultPrevented &&
event.button === 0 &&
!this.props.target &&
!isModifiedEvent(event)
) {
event.preventDefault();
// 使用<Router />組件提供的router實(shí)例
const { history } = this.context.router;
const { replace, to } = this.props;
if (replace) {
history.replace(to);
} else {
history.push(to);
}
}
};
render() {
const { replace, to, innerRef, ...props } = this.props;
// ...
const { history } = this.context.router;
const location =
typeof to === "string"
? createLocation(to, null, null, history.location)
: to;
const href = history.createHref(location);
return (
<a {...props} onClick={this.handleClick} href={href} ref={innerRef} />
);
}
}
<Link />
的核心就是渲染<a>
標(biāo)簽,攔截<a>
標(biāo)簽的點(diǎn)擊事件,然后通過(guò)<Router />
共享的router
對(duì)history
進(jìn)行路由操作,進(jìn)而通知<Router />
重新渲染。
// Route.js
/**
* The public API for matching a single path and rendering.
*/
class Route extends React.Component {
// ......
state = {
match: this.computeMatch(this.props, this.context.router)
};
// 計(jì)算匹配的路徑,匹配的話,會(huì)返回一個(gè)匹配對(duì)象,否則返回null
computeMatch(
{ computedMatch, location, path, strict, exact, sensitive },
router
) {
if (computedMatch) return computedMatch;
// ......
const { route } = router;
const pathname = (location || route.location).pathname;
return matchPath(pathname, { path, strict, exact, sensitive }, route.match);
}
// ......
render() {
const { match } = this.state;
const { children, component, render } = this.props;
const { history, route, staticContext } = this.context.router;
const location = this.props.location || route.location;
const props = { match, location, history, staticContext };
if (component) return match ? React.createElement(component, props) : null;
if (render) return match ? render(props) : null;
if (typeof children === "function") return children(props);
if (children && !isEmptyChildren(children))
return React.Children.only(children);
return null;
}
}
<Route />
有一部分源碼與<Router />
相似,可以實(shí)現(xiàn)路由的嵌套,但其核心是通過(guò)Context
共享的router
,判斷是否匹配當(dāng)前路由的路徑,然后渲染組件。
通過(guò)上述的分析,可以看出,整個(gè)react-router其實(shí)就是圍繞著<Router />
的Context
來(lái)構(gòu)建的。
使用Context開發(fā)組件
之前,通過(guò)Context
開發(fā)過(guò)一個(gè)簡(jiǎn)單的組件,插槽分發(fā)組件。本章就借著這個(gè)插槽分發(fā)組件的開發(fā)經(jīng)歷,聊聊如何使用Context
進(jìn)行組件的開發(fā)。
插槽分發(fā)組件
首先說(shuō)說(shuō)什么是插槽分發(fā)組件,這個(gè)概念最初是在Vuejs中認(rèn)識(shí)的。插槽分發(fā)是一種通過(guò)組件的組合,將父組件的內(nèi)容插入到子組件模板的技術(shù),在Vuejs中叫做Slot
。
為了讓大家更加直觀的理解這個(gè)概念,我從Vuejs搬運(yùn)了一段關(guān)于插槽分發(fā)的Demo。
對(duì)于提供的插槽的組件<my-component />
,模板如下:
<div>
<h2>我是子組件的標(biāo)題</h2>
<slot>
只有在沒(méi)有要分發(fā)的內(nèi)容時(shí)顯示
</slot>
</div>
對(duì)于父組件,模板如下:
<div>
<h1>我是父組件的標(biāo)題</h1>
<my-component>
<p>這是一些初始內(nèi)容</p>
<p>這是更多的初始內(nèi)容</p>
</my-component>
</div>
最終渲染的結(jié)果:
<div>
<h1>我是父組件的標(biāo)題</h1>
<div>
<h2>我是子組件的標(biāo)題</h2>
<p>這是一些初始內(nèi)容</p>
<p>這是更多的初始內(nèi)容</p>
</div>
</div>
可以看到組件<my-component />
的<slot />
節(jié)點(diǎn)最終被父組件中<my-component />
節(jié)點(diǎn)下的內(nèi)容所替換。
Vuejs還支持具名插槽。
例如,一個(gè)布局組件<app-layout />
:
<div class="container">
<header>
<slot name="header"></slot>
</header>
<main>
<slot></slot>
</main>
<footer>
<slot name="footer"></slot>
</footer>
</div>
而在父組件模板中:
<app-layout>
<h1 slot="header">這里可能是一個(gè)頁(yè)面標(biāo)題</h1>
<p>主要內(nèi)容的一個(gè)段落。</p>
<p>另一個(gè)段落。</p>
<p slot="footer">這里有一些聯(lián)系信息</p>
</app-layout>
最終渲染的結(jié)果:
<div class="container">
<header>
<h1>這里可能是一個(gè)頁(yè)面標(biāo)題</h1>
</header>
<main>
<p>主要內(nèi)容的一個(gè)段落。</p>
<p>另一個(gè)段落。</p>
</main>
<footer>
<p>這里有一些聯(lián)系信息</p>
</footer>
</div>
插槽分發(fā)的好處體現(xiàn)在,它可以讓組件具有可抽象成模板的能力。組件自身只關(guān)心模板結(jié)構(gòu),具體的內(nèi)容交給父組件去處理,同時(shí),不打破HTML描述DOM結(jié)構(gòu)的語(yǔ)法表達(dá)方式。我覺得這是一項(xiàng)很有意義的技術(shù),可惜,React對(duì)于這項(xiàng)技術(shù)的支持不是那么友好。于是我便參考Vuejs的插槽分發(fā)組件,開發(fā)了一套基于React的插槽分發(fā)組件,可以讓React組件也具模板化的能力。
對(duì)于<AppLayout />
組件,我希望可以寫成下面這樣:
class AppLayout extends React.Component {
static displayName = 'AppLayout'
render () {
return (
<div class="container">
<header>
<Slot name="header"></Slot>
</header>
<main>
<Slot></Slot>
</main>
<footer>
<Slot name="footer"></Slot>
</footer>
</div>
)
}
}
在外層使用時(shí),可以寫成這樣:
<AppLayout>
<AddOn slot="header">
<h1>這里可能是一個(gè)頁(yè)面標(biāo)題</h1>
</AddOn>
<AddOn>
<p>主要內(nèi)容的一個(gè)段落。</p>
<p>另一個(gè)段落。</p>
</AddOn>
<AddOn slot="footer">
<p>這里有一些聯(lián)系信息</p>
</AddOn>
</AppLayout>
組件的實(shí)現(xiàn)思路
根據(jù)前面所想的,先整理一下實(shí)現(xiàn)思路。
不難看出,插槽分發(fā)組件需要依靠?jī)蓚€(gè)子組件——插槽組件<Slot />
和分發(fā)組件<AddOn />
。插槽組件,負(fù)責(zé)打樁,提供分發(fā)內(nèi)容的坑位。分發(fā)組件,負(fù)責(zé)收集分發(fā)內(nèi)容,并提供給插槽組件去渲染分發(fā)內(nèi)容,相當(dāng)于插槽的消費(fèi)者。
顯然,這里遇到了一個(gè)問(wèn)題,<Slot />
組件與<AddOn />
組件是獨(dú)立的,如何將<AddOn />
的內(nèi)容填充到<Slot />
中呢?解決這個(gè)問(wèn)題不難,兩個(gè)獨(dú)立的模塊需要建立聯(lián)系,就給他們建立一個(gè)橋梁。那么這個(gè)橋梁要如何搭建呢?回過(guò)頭來(lái)看看之前的設(shè)想的代碼。
對(duì)于<AppLayout />
組件,希望寫成下面這樣:
class AppLayout extends React.Component {
static displayName = 'AppLayout'
render () {
return (
<div class="container">
<header>
<Slot name="header"></Slot>
</header>
<main>
<Slot></Slot>
</main>
<footer>
<Slot name="footer"></Slot>
</footer>
</div>
)
}
}
在外層使用時(shí),寫成這樣:
<AppLayout>
<AddOn slot="header">
<h1>這里可能是一個(gè)頁(yè)面標(biāo)題</h1>
</AddOn>
<AddOn>
<p>主要內(nèi)容的一個(gè)段落。</p>
<p>另一個(gè)段落。</p>
</AddOn>
<AddOn slot="footer">
<p>這里有一些聯(lián)系信息</p>
</AddOn>
</AppLayout>
無(wú)論是<Slot />
還是<AddOn />
,其實(shí)都在<AppLayout />
的作用域內(nèi)。<Slot />
是<AppLayout />
組件render()
方法返回的組件節(jié)點(diǎn),而<AddOn />
則是<AppLayout />
的children
節(jié)點(diǎn),所以,可以將<AppLayout />
視為<Slot />
與<AddOn />
的橋梁的角色。那么,<AppLayout />
通過(guò)什么給<Slot />
和<AddOn />
建立聯(lián)系呢?這里就用到本文的主角——Context
。接下來(lái)的問(wèn)題就是,如何使用Context
給<Slot />
和<AddOn />
建立聯(lián)系?
前面提到了<AppLayout />
這座橋梁。在外層組件,<AppLayout />
負(fù)責(zé)通過(guò)<AddOn />
收集為插槽填充的內(nèi)容。<AppLayout />
自身借助Context
定義一個(gè)獲取填充內(nèi)容的接口。在渲染的時(shí)候,因?yàn)?code><Slot />是<AppLayout />
渲染的節(jié)點(diǎn),所以,<Slot />
可以通過(guò)Context
獲取到<AppLayout />
定義的獲取填充內(nèi)容的接口,然后通過(guò)這個(gè)接口,獲取到填充內(nèi)容進(jìn)行渲染。
按照思路實(shí)現(xiàn)插槽分發(fā)組件
由于<AddOn />
是<AppLayout />
的children
節(jié)點(diǎn),并且<AddOn />
是特定的組件,我們可以通過(guò)name
或者displayName
識(shí)別出來(lái),所以,<AppLayout />
在渲染之前,也就是render()
的return
之前,對(duì)children
進(jìn)行遍歷,以slot
的值作為key
,將每一個(gè)<AddOn />
的children
緩存下來(lái)。如果<AddOn />
沒(méi)有設(shè)置slot
,那么將其視為給非具名的<Slot />
填充內(nèi)容,我們可以給這些非具名的插槽定一個(gè)key
,比如叫$$default
。
對(duì)于<AppLayout />
,代碼大致如下:
class AppLayout extends React.Component {
static childContextTypes = {
requestAddOnRenderer: PropTypes.func
}
// 用于緩存每個(gè)<AddOn />的內(nèi)容
addOnRenderers = {}
// 通過(guò)Context為子節(jié)點(diǎn)提供接口
getChildContext () {
const requestAddOnRenderer = (name) => {
if (!this.addOnRenderers[name]) {
return undefined
}
return () => (
this.addOnRenderers[name]
)
}
return {
requestAddOnRenderer
}
}
render () {
const {
children,
...restProps
} = this.props
if (children) {
// 以k-v的方式緩存<AddOn />的內(nèi)容
const arr = React.Children.toArray(children)
const nameChecked = []
this.addOnRenderers = {}
arr.forEach(item => {
const itemType = item.type
if (item.type.displayName === 'AddOn') {
const slotName = item.props.slot || '$$default'
// 確保內(nèi)容唯一性
if (nameChecked.findIndex(item => item === stubName) !== -1) {
throw new Error(`Slot(${slotName}) has been occupied`)
}
this.addOnRenderers[stubName] = item.props.children
nameChecked.push(stubName)
}
})
}
return (
<div class="container">
<header>
<Slot name="header"></Slot>
</header>
<main>
<Slot></Slot>
</main>
<footer>
<Slot name="footer"></Slot>
</footer>
</div>
)
}
}
<AppLayout />
定義了一個(gè)Context
接口requestAddOnRenderer()
,requestAddOnRenderer()
接口根據(jù)name
返回一個(gè)函數(shù),這個(gè)返回的函數(shù)會(huì)根據(jù)name
訪問(wèn)addOnRenderers
的屬性,addOnRenderers
就是<AddOn />
的內(nèi)容緩存對(duì)象。
<Slot />
的實(shí)現(xiàn)很簡(jiǎn)單,代碼如下:
// props, context
const Slot = ({ name, children }, { requestAddOnRenderer }) => {
const addOnRenderer = requestAddOnRenderer(name)
return (addOnRenderer && addOnRenderer()) ||
children ||
null
}
Slot.displayName = 'Slot'
Slot.contextTypes = { requestAddOnRenderer: PropTypes.func }
Slot.propTypes = { name: PropTypes.string }
Slot.defaultProps = { name: '$$default' }
可以看到<Slot />
通過(guò)context
獲取到<AppLayout />
提供的接口requestAddOnRenderer()
,最終渲染的主要對(duì)象就是緩存在<AppLayout />
中的<AddOn />
的內(nèi)容。如果沒(méi)有獲取到指定的<AddOn />
的內(nèi)容,則渲染<Slot />
自身的children
。
<AddOn />
更簡(jiǎn)單:
const AddOn = () => null
AddOn.propTypes = { slot: PropTypes.string }
AddOn.defaultTypes = { slot: '$$default' }
AddOn.displayName = 'AddOn'
<AddOn />
不做任何事情,僅僅返回null
,它的作用就是讓<AppLayout />
緩存分發(fā)給插槽的內(nèi)容。
可以讓<AppLayout />
更具通用性
通過(guò)上文的代碼,基本將<AppLayout />
改造成了一個(gè)具備插槽分發(fā)能力的組件,但是很明顯的,<AppLayout />
并不具備通用性,我們可以將它提升成一個(gè)獨(dú)立通用的組件。
我給這個(gè)組件命名為SlotProvider
function getDisplayName (component) {
return component.displayName || component.name || 'component'
}
const slotProviderHoC = (WrappedComponent) => {
return class extends React.Component {
static displayName = `SlotProvider(${getDisplayName(WrappedComponent)})`
static childContextTypes = {
requestAddOnRenderer: PropTypes.func
}
// 用于緩存每個(gè)<AddOn />的內(nèi)容
addOnRenderers = {}
// 通過(guò)Context為子節(jié)點(diǎn)提供接口
getChildContext () {
const requestAddOnRenderer = (name) => {
if (!this.addOnRenderers[name]) {
return undefined
}
return () => (
this.addOnRenderers[name]
)
}
return {
requestAddOnRenderer
}
}
render () {
const {
children,
...restProps
} = this.props
if (children) {
// 以k-v的方式緩存<AddOn />的內(nèi)容
const arr = React.Children.toArray(children)
const nameChecked = []
this.addOnRenderers = {}
arr.forEach(item => {
const itemType = item.type
if (item.type.displayName === 'AddOn') {
const slotName = item.props.slot || '$$default'
// 確保內(nèi)容唯一性
if (nameChecked.findIndex(item => item === stubName) !== -1) {
throw new Error(`Slot(${slotName}) has been occupied`)
}
this.addOnRenderers[stubName] = item.props.children
nameChecked.push(stubName)
}
})
}
return (<WrappedComponent {...restProps} />)
}
}
}
export const SlotProvider = slotProviderHoC
使用React的高階組件對(duì)原來(lái)的<AppLayout />
進(jìn)行改造,將其轉(zhuǎn)變?yōu)橐粋€(gè)獨(dú)立通用的組件。對(duì)于原來(lái)的<AppLayout />
,可以使用這個(gè)SlotProvider
高階組件,轉(zhuǎn)換成一個(gè)具備插槽分發(fā)能力的組件。
import { SlotProvider } from './SlotProvider.js'
class AppLayout extends React.Component {
static displayName = 'AppLayout'
render () {
return (
<div class="container">
<header>
<Slot name="header"></Slot>
</header>
<main>
<Slot></Slot>
</main>
<footer>
<Slot name="footer"></Slot>
</footer>
</div>
)
}
}
export default SlotProvider(AppLayout)
通過(guò)以上的經(jīng)歷,可以看到,當(dāng)設(shè)計(jì)開發(fā)一個(gè)組件時(shí),
-
組件可能需要由一個(gè)根組件和多個(gè)子組件一起合作來(lái)完成組件功能。比如插槽分發(fā)組件實(shí)際上需要
SlotProvider
與<Slot />
和<AddOn />
一起配合使用,SlotProvider
作為根組件,而<Slot />
和<AddOn />
都算是子組件。 -
子組件相對(duì)于根組件的位置或者子組件之間的位置是不確定。對(duì)于
SlotProvider
而言,<Slot />
的位置是不確定的,它會(huì)處在被SlotProvider
這個(gè)高階組件所包裹的組件的模板的任何位置,而對(duì)于<Slot />
和<AddOn />
,他們直接的位置也不確定,一個(gè)在SlotProvider
包裝的組件的內(nèi)部,另一個(gè)是SlotProvider
的children
。 -
子組件之間需要依賴一些全局態(tài)的API或者數(shù)據(jù),比如
<Slot />
實(shí)際渲染的內(nèi)容來(lái)自于SlotProvider
收集到的<AddOn />
的內(nèi)容。
這時(shí)我們就需要借助一個(gè)中間者作為媒介來(lái)共享數(shù)據(jù),相比額外引入redux這些第三方模塊,直接使用Context
可以更優(yōu)雅。
嘗試一下新版本的Context API
使用新版的Context API對(duì)之前的插槽分發(fā)組件進(jìn)行改造。
// SlotProvider.js
function getDisplayName (component) {
return component.displayName || component.name || 'component'
}
export const SlotContext = React.createContext({
requestAddOnRenderer: () => {}
})
const slotProviderHoC = (WrappedComponent) => {
return class extends React.Component {
static displayName = `SlotProvider(${getDisplayName(WrappedComponent)})`
// 用于緩存每個(gè)<AddOn />的內(nèi)容
addOnRenderers = {}
requestAddOnRenderer = (name) => {
if (!this.addOnRenderers[name]) {
return undefined
}
return () => (
this.addOnRenderers[name]
)
}
render () {
const {
children,
...restProps
} = this.props
if (children) {
// 以k-v的方式緩存<AddOn />的內(nèi)容
const arr = React.Children.toArray(children)
const nameChecked = []
this.addOnRenderers = {}
arr.forEach(item => {
const itemType = item.type
if (item.type.displayName === 'AddOn') {
const slotName = item.props.slot || '$$default'
// 確保內(nèi)容唯一性
if (nameChecked.findIndex(item => item === stubName) !== -1) {
throw new Error(`Slot(${slotName}) has been occupied`)
}
this.addOnRenderers[stubName] = item.props.children
nameChecked.push(stubName)
}
})
}
return (
<SlotContext.Provider value={
requestAddOnRenderer: this.requestAddOnRenderer
}>
<WrappedComponent {...restProps} />
</SlotContext.Provider>
)
}
}
}
export const SlotProvider = slotProviderHoC
移除了之前的childContextTypes
和getChildContext()
,除了局部的調(diào)整,整體核心的東西沒(méi)有大變化。
// Slot.js
import { SlotContext } from './SlotProvider.js'
const Slot = ({ name, children }) => {
return (
<SlotContext.Consumer>
{(context) => {
const addOnRenderer = requestAddOnRenderer(name)
return (addOnRenderer && addOnRenderer()) ||
children ||
null
}}
</SlotContext.Consumer>
)
}
Slot.displayName = 'Slot'
Slot.propTypes = { name: PropTypes.string }
Slot.defaultProps = { name: '$$default' }
由于之前就按照生產(chǎn)者消費(fèi)者的模式來(lái)使用Context
,加上組件自身也比較簡(jiǎn)單,因此使用新的API進(jìn)行改造后,差別不大。
總結(jié)
- 相比
props
和state
,React的Context
可以實(shí)現(xiàn)跨層級(jí)的組件通信。 - Context API的使用基于生產(chǎn)者消費(fèi)者模式。生產(chǎn)者一方,通過(guò)組件靜態(tài)屬性
childContextTypes
聲明,然后通過(guò)實(shí)例方法getChildContext()
創(chuàng)建Context
對(duì)象。消費(fèi)者一方,通過(guò)組件靜態(tài)屬性contextTypes
申請(qǐng)要用到的Context
屬性,然后通過(guò)實(shí)例的context
訪問(wèn)Context
的屬性。 - 使用
Context
需要多一些思考,不建議在App中使用Context
,但如果開發(fā)組件過(guò)程中可以確保組件的內(nèi)聚性,可控可維護(hù),不破壞組件樹的依賴關(guān)系,影響范圍小,可以考慮使用Context
解決一些問(wèn)題。 - 通過(guò)
Context
暴露API或許在一定程度上給解決一些問(wèn)題帶來(lái)便利,但個(gè)人認(rèn)為不是一個(gè)很好的實(shí)踐,需要慎重。 - 舊版本的
Context
的更新需要依賴setState()
,是不可靠的,不過(guò)這個(gè)問(wèn)題在新版的API中得以解決。 - 可以把
Context
當(dāng)做組件的作用域來(lái)看待,但是需要關(guān)注Context
的可控性和影響范圍,使用之前,先分析是否真的有必要使用,避免過(guò)度使用所帶來(lái)的一些副作用。 - 可以把
Context
當(dāng)做媒介,進(jìn)行App級(jí)或者組件級(jí)的數(shù)據(jù)共享。 - 設(shè)計(jì)開發(fā)一個(gè)組件,如果這個(gè)組件需要多個(gè)組件關(guān)聯(lián)組合的,使用
Context
或許可以更加優(yōu)雅。
以上是我的分享內(nèi)容,如有不足或者錯(cuò)誤的地方,歡迎批評(píng)指正。
引用
- Context - https://reactjs.org/docs/context.html
- React 16.3來(lái)了:帶著全新的Context API - http://cnodejs.org/topic/5a7bd5c4497a08f571384f03
- Content Distribution with Slots - https://vuejs.org/v2/guide/components.html#Content-Distribution-with-Slots