react 常见api 使用(长期更新)
1、父子通信
1.1 父-子 props
父组件:
class myPage extends React.Component {
render() {
return (
<div>
{/* 子组件 自定义page 是子组件要接受的属性 mypage是要传递的内容*/}
<TabBar page="mypage"></TabBar>
<div className="pd-md">
我是一个mypage
</div>
</div>
);
}
}
子组件:
class TabBarComponents extends React.Component {
constructor(props) {
// 继承父组件
super(props);
this.state = {
// 接受父组件传的属性
selectedTab: props.page,
};
}
// 然后使用 this.state.selectedTab 这个值, 这个就是mypage
....
}
1.2
子-》父
子组件:将子组件的value值 text 传递给父组件
class Input extends Component {
changeTitle(event) {
// 子组件调用 父组件通过props传递过来的方法 onChangeText,将要传递的值传进方法
this.props.onChangeText(event)
}
componentDidMount() {
this.props.getParentTab(1)
}
render() {
return (
<div className="list-wrapper">
<input type="text" onChange={this.changeTitle.bind(this)} value={this.props.text} />
<input type="text" onChange={this.props.getParentTab('2')} value={this.props.text} />
</div>
);
}
}
父组件:
class myPage extends React.Component {
constructor(props) {
super(props);
this.state = {newText: 'default'};
}
changeText(event) {
this.setState({
newText: event.target.value,
})
}
getParentTab = (tab) => {
console.log(tab)
}
render() {
return (
<div>
<div className="pd-md">
<h3>我是一个mypage</h3>
<div>
{this.state.newText}
{/* 子组件 */}
<InputCompenent onChangeText={this.changeText.bind(this)} text={this.state.newText}></InputCompenent>
<SlideFirst className="content" getParentTab={getParentTab} />
</div>
</div>
</div>
);
}
}
子组件通过调用 props.onChangeText 方法,将值传递进来,父组件通过 changeText 方法来接受 传递进来的值。
套路:子组件通过调用 父组件传递的方法 传值。
父组件调用子组件的方法:
父组件
onRef = (ref) => {
this.child = ref
}
addEtcItem = () => {
this.child.add(); // 调用子组件的方法
}
....
<ChildItem
onRef={this.onRef}
/>
子组件:
add = () => {
// todo
};
2、在标签上写样式的方法
{
<div style={{
display: this.state.hasMore ? 'none' : 'block',
textAlign: 'center',
borderTop: '1px solid #ddd',
width: '80%',
paddingTop: '15px',
marginLeft: 'auto',
marginRight: 'auto',
marginBottom: '70px'
}}>
不要扯了,已经到底了!
</div>
}
3、input type="file" onchange 获取 e.target
DOM:
<input type="file" id="uploadExcel" onChange={this.uploadFiles} className="beauty-input"/>
js:
uploadFiles = (e) => {
e.persist(); // 不然e.target 为null
console.log(e)
....
react 常见api 使用(长期更新)的更多相关文章
- python的多版本安装以及常见错误(长期更新)
(此文长期更新)Python安装常见错误汇总 注:本教程以python3.6为基准 既然是总结安装过程中遇到的错误,就顺便记录一下我的安装过程好了. 先来列举一下安装python3.6过程中可能需要的 ...
- 常用开放api【长期更新】
获取时间: 苏宁:http://quan.suning.com/getSysTime.do 淘宝:http://api.m.taobao.com/rest/api3.do?api=mtop.commo ...
- Java代码优化(长期更新)
前言 2016年3月修改,结合自己的工作和平时学习的体验重新谈一下为什么要进行代码优化.在修改之前,我的说法是这样的: 就像鲸鱼吃虾米一样,也许吃一个两个虾米对于鲸鱼来说作用不大,但是吃的虾米多了,鲸 ...
- React Router API文档
React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...
- React 组件 API
React 组件 API 在本章节中我们将讨论 React 组件 API.我们将讲解以下7个方法: 设置状态:setState 替换状态:replaceState 设置属性:setProps 替换属性 ...
- iOS-工作经验+资料分享(长期更新)
在此记录工作中的一些经验和技术资料 长期更新 欢迎各位业内朋友指正.交流技术上的问题 0.苹果开发联盟电话 4006 701855 1.轻易不用使用tableViewController,因为改变他自 ...
- 免费api大全(更新中)
免费api大全(更新中) API大全 http://www.apidq.com/ (这个碉堡了) 天气接口 气象局接口 完整数据:http://m.weather.com.cn/data/10 ...
- 【quickhybrid】API的分类:短期API、长期API
前言 一切就绪,开始规划API,这里在规划前对API进行了一次分类:短期API.长期API 首先申明下,这个是在实际框架演变过程中自创的一个概念,其它混合框架可能也会有这个概念,但应该是会在原生底层来 ...
- Ubuntu16.04系统美化、常用软件安装等,长期更新
Ubuntu16.04系统美化.常用软件安装等,长期更新 IT之家啊 18-09-0915:00 因为我个人偏向于玩VPS.服务器之类的东西,所以一般我都是用CentOS.不过对于桌面版的Linux, ...
随机推荐
- Unity编辑器扩展
在开发中有可能需要自己开发编辑器工具,在Unity中界面扩展常见两种情况,拿某插件为例: 1,自建窗口扩展 2,脚本Inspector显示扩展 不管使用那种样式,都需要经常用到两个类EditorGUI ...
- 手动安装Package Control
手动下载一个package control的包:https://github.com/wbond/package_control 然后Download ZIP后,解压,将解压后的文件夹重命名为 Pac ...
- linux cp 直接覆盖不提示信息 解决方法
默认情况下,cp覆盖时,无论加什么参数 -f 之类的 还是提示是否覆盖. 原因是:服务器会默认增加别名 alias cp=’cp -i’,当你执行cp时,其实执行的是cp –i. [root@ltt0 ...
- 1.nginx_add_after_body
语法: add_before_body uri;默认值: —配置段: http, server, location发起一个子请求,请求给定的uri,并且将内容追加到主题响应的内容之前. 语法: add ...
- Adding appsettings.json to a .NET Core console app
This is something that strangely doesn’t seem to be that well documented and took me a while to figu ...
- 代理与hook
参考:Java 动态代理 代理是什么 为什么需要代理呢?其实这个代理与日常生活中的“代理”,“中介”差不多:比如你想海淘买东西,总不可能亲自飞到国外去购物吧,这时候我们使用第三方海淘服务比如惠惠购物助 ...
- Fedora 24系统基本命令
Fedora 24基本命令 一. DNF软件管理 1. 修改配置:在/etc/dnf/dnf.conf中加入fastestmirror=true.keepcache=true ...
- 用js提取字符串中的某一段字符
String.prototype.getQuery = function(name){var reg = new RegExp("(^|&)"+ name +"= ...
- 【JS】前端文件下载(无刷新)方法总结
#传统方法 利用iframe 或 form.submit 或 windows.open直接向后端发请求,后端返回文件流,后端处理成功后会直接返回到页面,浏览器会整理并打开自己的保存下载文件机制 . 1 ...
- 数据结构与算法之美学习笔记:B+树(第48讲)
一.解决问题的前提是定义清楚问题 通过对一些模糊需求进行假设,来限定要解决问题的范围 根据某个值查找数据,比如 select * from use where id=1234: 根据区间值来查询某些数 ...