2.1
for循環(huán)時(shí),可以這么寫
(let i = 0, length = children.length; i < length; i++)
propTypes.arrayOf(propTypes.oneOfType([propTypes.string, propTypes.number]))
父組件
static propTypes = { onSelected: propTypes.func }
static childContextTypes = { onSelected: propTypes.func }
getChildContext() { const onSelected = _onSelected.bind(this) const context = { onSelected }; return Object.freeze(context); }
子組件
static contextTypes = { onSelected: propTypes.func }
_onSelected = () => { this.context.onSelected() }
<a onClick={this._onSelected.bind(this)}/>
總結(jié):context 使用一般適用于多重組件嵌套或者子組件需要從祖先組件取值或者觸發(fā)祖先組件的方法,步驟:首先在祖先組件定義類型,再定義需要傳出去的方法或者值,在子組件中先定義,然后用this.context使用。