Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React-如何使用cloneElement以保留上下文_Reactjs - Fatal编程技术网

Reactjs React-如何使用cloneElement以保留上下文

Reactjs React-如何使用cloneElement以保留上下文,reactjs,Reactjs,我正在使用React的未记录上下文功能。我注意到有一个警告,即只有显式调用组件时,此上下文功能才有效: render() { return <div><SomeComponent /></div> } render(){ 返回 } 但是,如果克隆项目,则不会传递上下文: render() { return <div>{React.cloneElement(this.props.content)}</div> } rende

我正在使用React的未记录上下文功能。我注意到有一个警告,即只有显式调用组件时,此上下文功能才有效:

render() {
  return <div><SomeComponent /></div>
}
render(){
返回
}
但是,如果克隆项目,则不会传递上下文:

render() {
  return <div>{React.cloneElement(this.props.content)}</div>
}
render(){
返回{React.cloneElement(this.props.content)}
}

这是虫子吗?还是有什么方法可以让这项工作在我不知道的情况下进行?

啊哈。显然在0.14之前,您需要使用
React.addons.cloneWithProps
来保存上下文。在0.14及以上时,
反应。cloneElement
应完成此工作

参考: