React 关于组件(界面)更新
在最近在学 React , 将组件的UI更新稍微整理了一下。
根据业务要求,可能会出现如下的技术实现要求:
1.更新自己
2.更新子组件
3.更新兄弟组件
4.更新父组件
5.父 call 子 function
6.子 call 父 function
整理代码如下:
更新自己:
import React, { Component } from 'react';
import { Button } from 'antd';
class Me extends Component {
constructor(props){
super(props)
this.state = { Val : }
this.handleClick = this.handleClick.bind( this );
}
handleClick(e) {
e.preventDefault();
console.log('The link was clicked.');
this.setState({
Val:
});
}
render() {
return (
<div style={{ width:, height:, backgroundColor:"#FF0000" }} >
<span> myValue { this.state.Val } aaa</span>
<Button onClick={ this.handleClick } >更新自己 </Button>
</div>
);
}
}
export default Me;
更新儿子
class Son extends Component {
constructor(props){
super(props)
//this.state = { SonVal :10 }
}
render() {
return (
<div style={{ width:, height:, backgroundColor:"#00FF00" }} >
<span> sonValue { this.props.SonVal } aaa</span>
</div>
);
}
}
export default Son;
class Me extends Component {
constructor(props){
super(props)
this.state = { Val :, SonVal : }
this.handleClick = this.handleClick.bind( this );
this.handleClick4Son = this.handleClick4Son.bind( this );
}
handleClick(e) {
e.preventDefault();
console.log('The link was clicked.');
this.setState({
Val: this.state.Val+
});
}
handleClick4Son(e) {
e.preventDefault();
this.setState({
SonVal: this.state.SonVal+
});
}
render() {
return (
<div style={{ width:, height:, backgroundColor:"#FF0000" }} >
<span> myValue { this.state.Val } aaa</span>
<Button onClick={ this.handleClick } >更新自己 </Button>
<Button onClick={ this.handleClick4Son } >更新儿子 </Button>
<Son SonVal={ this.state.SonVal + } />
</div>
);
}
}
更新兄弟:
更新兄弟, 就需要和老爹相关了, 老爹组件 App (只是命名,可以叫其他) :
import Me from './component/Me'
import Brother from "./component/Brother"; class App extends Component { constructor(props){
super(props)
this.state = { My2SonVal :30 }
this.onMy2SonValChangle = this.onMy2SonValChangle.bind( this ); } onMy2SonValChangle(e) { this.setState({
My2SonVal: e
});
} render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p> <Me chagleBrother={ this.onMy2SonValChangle } />
<Brother BrotherVal={ this.state.My2SonVal }/> </div>
);
}
}
兄弟:
class Brother extends Component {
constructor(props){
super(props)
//this.state = { SonVal :10 }
}
render() {
return (
<div style={{ width:, height:, backgroundColor:"#00FFFF" }} >
<span> brotherValue { this.props.BrotherVal } </span>
</div>
);
}
}
export default Brother;
自己:
class Me extends Component {
constructor(props){
super(props)
this.state = { Val :, SonVal :, BrotherVal: }
this.handleClick = this.handleClick.bind( this );
this.handleClick4Son = this.handleClick4Son.bind( this );
this.handleClick4Brother = this.handleClick4Brother.bind( this );
}
handleClick(e) {
e.preventDefault();
console.log('The link was clicked.');
this.setState({
Val: this.state.Val+
});
}
handleClick4Son(e) {
e.preventDefault();
this.setState({
SonVal: this.state.SonVal+
});
}
handleClick4Brother(e) {
e.preventDefault();
this.state.BrotherVal = this.state.BrotherVal+
this.props.chagleBrother( this.state.BrotherVal )
}
render() {
return (
<div style={{ width:, height:, backgroundColor:"#FF0000" }} >
<span> myValue { this.state.Val } aaa</span>
<Button onClick={ this.handleClick } >更新自己 </Button>
<Button onClick={ this.handleClick4Son } >更新儿子 </Button>
<Button onClick={ this.handleClick4Brother } >更新兄弟 </Button>
<Son SonVal={ this.state.SonVal + } />
</div>
);
}
}
export default Me;
自此我们已经完成了: 更新自己, 更新子组件,更新兄弟组件,更新父组件(调整一下更新兄弟组件代码), 子 call 父 function
还需要完成: 父 call 子
class Me2 extends Component {
constructor(props){
super(props)
this.onSetChild = this.onSetChild.bind(this);
this.handleClick = this.handleClick.bind(this);
}
render() {
return(
<div styles = { { width :, height:, backgroundColor:"#4400FF" } }>
<Child fatherCall={ this.onSetChild } />
<button onClick={this.handleClick} >click</button>
</div>
)
}
onSetChild( childObj ){
this.child = childObj;
}
handleClick() {
this.child.sonFunction()
}
}
class Child extends Component {
componentDidMount(){
this.props.fatherCall(this)
}
sonFunction(){
console.log('sonFunction --------- ')
}
render() {
return ( <div> son Txt </div> )
}
};
export default Me2;
React 关于组件(界面)更新的更多相关文章
- 从 React 的组件更新谈 Immutable 的应用
在介绍 Immutable 如何在 React 中应用之前,先来谈谈 React 组件是如何更新的. React 是基于状态驱动的开发,可以将一个组件看成是一个有限状态机,组件要更新,必须更新状态. ...
- React Native组件、生命周期及属性传值props详解
创建组件的三种方式 第一种:通过ES6的方式创建 /** * 方式一 :ES6 */ export default class HelloComponent extends Component { r ...
- React 面向组件化编程 - 封装了webpack - npm run build 产生的包的 /static 引用路径问题
React 面向组件化编程 面向对象 ----> 面向模块 ----> 面向组件 套路: 注意: 组件名必须大写开头: 只能有一个根标签: <input />虚拟DOM 元素必 ...
- react native组件的生命周期
react native组件的生命周期 一.当页面第一次加载时,会依次调用: constructor() componentWillMount(): 这个函数调用时机是在组件创建,并初始化了状态之后, ...
- React Native组件(二)View组件解析
相关文章 React Native探索系列 React Native组件系列 前言 了解了RN的组件的生命周期后,我们接着来学习RN的具体的组件.View组件是最基本的组件,也是首先要掌握的组件,这一 ...
- Android React Native组件的生命周期及回调函数
熟悉android的童鞋应该都清楚,android是有生命周期的,其很多组件也是有生命周期.今天小编和大家分享的React Native组件的生命周期,还不了解的童鞋,赶紧来围观吧 在android开 ...
- React: React的组件状态机制
一.简介 在React中,有两个核心的默认属性,分别是state和props.state会记录组件的状态,React根据状态的变化,会对界面做相应的调整或渲染.props则是数据流向属性,React通 ...
- React: 研究React的组件化
一.简介大概 在以往的Web开发中,会把web页面所有的复杂控件作为一个单一的整体进行开发,由于控件之间需要进行通信,因此不同的组件之间的耦合度会很多,由于开发一个控件的时候要考虑到控件与控件之间的联 ...
- React 之 组件生命周期
React 之 组件生命周期 理解1) 组件对象从创建到死亡它会经历特定的生命周期阶段2) React组件对象包含一系列的勾子函数(生命周期回调函数), 在生命周期特定时刻回调3) 我们在定义组件时, ...
随机推荐
- AngularJS1.X学习笔记10-自定义指令(下)
继续继续,学完这个部分就去吃饭.引用自由男人的话作为本文的开始:“默认情况下,链接函数被传入了控制器的作用域,而该控制器管理着的视图包含了指令所应用到的元素”.果然像是绕口令,还是看看你的例子比较好. ...
- HTTP请求到爬虫代码的终南捷径
前阵子在做爬虫的时候学会了各种抓包,看到http请求的时候硬拼代码实在有点累. 后来发现Postman工具是直接可以把Postman请求直接生成对应的代码,这样一下来就美滋滋了. 那么最后的问题就成了 ...
- 新概念英语(1-95)Tickets,please!
Lesson 95 Tickets, please. 请把车票拿出来. Listen to the tape then answer this question. Why did George and ...
- Node.js初探之实现能向前台返回东西的简单服务器
nodejs nodejs文件就是一个简单的js文件. 在shell中运行 Step 1. 打开终端,进入这个js文件所在目#录 Step 2. 用 'node 文件名.js' 命令运行它即可. 用n ...
- O(logN)中logN的底数
转载:http://blog.csdn.net/jdbc/article/details/42173751 问题: 无论是计算机算法概论.还是数据结构书中, 关于算法的时间复杂度很多都用包含O(log ...
- Docker学习(1)安装
1. Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱 ...
- SpringBoot(七):集成DataSource 与 Druid监控配置
绑定DataSource:Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource,Druid是Java语言中最好的数据库连接池,并且能够提供 ...
- canvas绘制圆心扇形可组成颜色随机的七色小花
啊~现在应该还是春天吧.心情一如既往的烦闷呐.最近做了一个canvas的扇形绘制的东西.把它整理出来变成一个适合春天的花朵绘制~沉闷的工作环境已经让我这种有趣的人也变成了无聊鬼怪呢.下次一定想找一个年 ...
- 文件上传详解 (HTML FILE)
FileUpload 对象 在 HTML 文档中 <input type="file"> 标签每出现一次,一个 FileUpload 对象就会被创建. 该元素包含一个文 ...
- .NET Core2.0+MVC 用Redis/Memory+cookie实现的sso单点登录
之前发布过使用session+cookie实现的单点登录,博主个人用的很不舒服,为什么呢,博主自己测试的时候,通过修改host的方法,在本机发布了三个站点,但是,经过测试,发现,三个站点使用的sess ...