[React] Use React.cloneElement to Extend Functionality of Children Components
We can utilize React.cloneElement in order to create new components with extended data or functionality.
class App extends React.Component {
render(){
return (
<Buttons>
<button value="A">A</button>
<button value="B">B</button>
<button value="C">C</button>
</Buttons>
)
}
}
class Buttons extends React.Component {
constructor(){
super();
this.state = {selected: 'None'}
}
selectItem(selected){
this.setState({selected})
}
render(){
let fn = child =>
React.cloneElement(child, {
onClick:this.selectItem.bind(this, child.props.value)
})
let items = React.Children.map(this.props.children, fn);
return (
<div>
<h2>You have selected: {this.state.selected}</h2>
{items}
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
We render {items} below h2 tag, and for each button element in Buttons, we are going to extend the element by adding a 'onClick' method to it.
[React] Use React.cloneElement to Extend Functionality of Children Components的更多相关文章
- 重谈react优势——react技术栈回顾
react刚刚推出的时候,讲react优势搜索结果是几十页. 现在,react已经慢慢退火,该用用react技术栈的已经使用上,填过多少坑,加过多少班,血泪控诉也不下千文. 今天,再谈一遍react优 ...
- React学习笔记-1-什么是react,react环境搭建以及第一个react实例
什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...
- 小谈React、React Native、React Web
React有三个东西,React JS 前端Web框架,React Native 移动终端Hybrid框架,React Web是一个源码转换工具(React Native 转 Web,并之所以特别提出 ...
- React的React Native
React的React Native React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React ...
- React Navigation & React Native & React Native Navigation
React Navigation & React Native & React Native Navigation React Navigation https://facebook. ...
- React 与 React Native 底层共识:React 是什么
此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React 与 React Native 结合学习的方法,此小节主要介绍 React 的底层原理与 ...
- 一次掌握 React 与 React Native 两个框架
此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React 与 React Native 结合学习的方法. 1. 软件开发语言与框架的学习本质 我 ...
- 《React Native 精解与实战》书籍连载「React 与 React Native 简介」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- [React] Validate React Forms with Formik and Yup
Validating forms in React can take several lines of code to build. However, Formik's ErrorMessage co ...
随机推荐
- 洛谷 【P1252】马拉松接力赛
洛谷 [P1252]马拉松接力赛 题目描述 某城市冬季举办环城25km马拉松接力赛,每个代表队有5人参加比赛,比赛要求每个的每名参赛选手只能跑一次,一次至少跑1km.最多只能跑10km,而且每个选手所 ...
- wap.css
wap.css 一.总结 1.官方有教程:英语的 http://www.developershome.com/wap/wcss/ 2.wap.css :就是控制页面在手机端样式的 3.DOCTYPE ...
- golang recover panic 流程控制的可达与不可达
--------------------------流程控制可达----------------------------- package main import "fmt" fu ...
- (错误记录)git push 报错 403
在push的时候遇到错误: RPC failed; HTTP curl The requested URL returned error: Forbidden 如果是自己创建的项目的话,可以在网上找到 ...
- Mongodb总结2-Java版本的HelloWorld-CRUD例子
2013年,写的CRUD太简单了,今天在原来的基础上,稍微完善了下,用了更多语法,比如排序sort.in语句等. 参考了<Mongodb权威指南-第1版-高清>,等下上传到CSDN下载频道 ...
- Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...
- <meta name="viewport" content="width=device-width,initial-scale=1.0">
meta name="viewport" content="width=device-width,initial-scale=1.0" 解释 <meta ...
- C++开源码项目汇总
Google的C++开源码项目 v8 - V8 JavaScript Engine V8 是 Google 的开源 JavaScript 引擎. V8 採用 C++ 编写,可在谷歌浏览器(来自 G ...
- numpy 细节问题
1. np.expand_dims >> X = np.random.randint(0, 9, (2, 3)) >> mean_X = np.mean(X, axis=0) ...
- 18、x264编码在zedboard上的实现(软编码)
一.x264开源包获取 x264-snapshot提供了开源x264源代码,已经在X86和ARM架构下均已实现.linux下可以使用git获得最新的代码包 git clone git://git.vi ...