React module methods with passing props to child or invoking callback to parent.
Some code samples for this pupose:
- import React from "react";
- import MyDemo from "./mydemo.jsx";
- export default class Square extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- count: 0
- };
- this.handleChange = this.handleChange.bind(this);
- this.changeMyStateFromChild = this.changeMyStateFromChild.bind(this);
- }
- componentDidMount() {
- let me = this;
- me.setState({
- count: me.state.count + 1
- });
- console.log(me.state.count); // 打印出0
- me.setState({
- count: me.state.count + 1
- });
- console.log(me.state.count); // 打印出0
- setTimeout(function() {
- me.setState({
- count: me.state.count + 1
- });
- console.log(me.state.count); // 打印出2
- }, 0);
- setTimeout(function() {
- me.setState({
- count: me.state.count + 1
- });
- console.log(me.state.count); // 打印出3
- }, 0);
- }
- handleChange(e) {
- let me = this;
- const target = e.target;
- console.log(me);
- alert(me.state.count);
- this.setState({
- [target.name]: target.value
- });
- console.log(MyDemo);
- }
- changeMyStateFromChild(state) {
- // this.setState(state);
- debugger;
- alert(state);
- }
- render() {
- return (
- <div onChange={e => this.handleChange(e)}>
- <MyDemo
- title={this.state.count}
- changeParent={this.changeMyStateFromChild}
- />
- <input type="text" name="username" />
- <input type="text" name="password" />
- <button onClick={() => alert(MyDemo.title)}>click </button>
- <h1>{this.state.count}</h1>
- </div>
- );
- }
- }
React module methods with passing props to child or invoking callback to parent.的更多相关文章
- React 深入系列3:Props 和 State
文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...
- React组件的state和props
React组件的state和props React的数据是自顶向下单向流动的,即从父组件到子组件中,组件的数据存储在props和state中.实际上在任何应用中,数据都是必不可少的,我们需要直接的改变 ...
- React LifeCycle Methods & re-learning 2019
React LifeCycle Methods & re-learning 2019 v16.9.0 https://reactjs.org/docs/react-component.html ...
- React组件三大属性之 props
React组件三大属性之 props 理解1) 每个组件对象都会有props(properties的简写)属性2) 组件标签的所有属性都保存在props中 作用1) 通过标签属性从组件外向组件内传递变 ...
- React报错之Parameter 'props' implicitly has an 'any' type
正文从这开始~ 总览 当我们没有为函数组件或者类组件的props声明类型,或忘记为React安装类型声明文件时,会产生"Parameter 'props' implicitly has an ...
- React中的State与Props
一.State 1.什么是 state 一个组件的显示形态可以由数据状态和外部参数决定,其中,数据状态为 state,外部参数为 props 2.state 的使用 组件初始化时,通过 this.st ...
- React 的组件与 this.props对象
1.组件 React 允许将代码封装成组件,然后像插入普通 HTML 标签一样,在网页中插入这个组件.React.createClass 的方法就是用于生成一个组件类. 2.this.props对象 ...
- [React] Use React.ReactNode for the children prop in React TypeScript components and Render Props
Because @types/react has to expose all its internal types, there can be a lot of confusion over how ...
- React.js 小书 Lesson22 - props.children 和容器类组件
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson22 转载请注明出处,保留原文链接和作者信息. 有一类组件,充当了容器的作用,它定义了一种外层 ...
随机推荐
- MyEclipse - 问题集 - 创建Maven项目,JDK版本默认是1.5
修改Maven的配置文件settings.xml,增加profile节点,如下所示: <profile> <id>jdk-1.8</id> <activati ...
- 机器学习实战一:kNN手写识别系统
实战一:kNN手写识别系统 本文将一步步地构造使用K-近邻分类器的手写识别系统.由于能力有限,这里构造的系统只能识别0-9.需要识别的数字已经使用图形处理软件,处理成具有相同的色彩和大小:32像素*3 ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)
Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...
- 并查集——poj2492(带权并查集入门)
一.题目回顾 题目链接:传送门 题意:给定n只虫子,不同性别的可以在一起,相同性别的不能在一起.给你m对虫子,判断中间有没有同性别在一起的. 二.解题思路 种类并查集 和poj1073的本质一样 详见 ...
- windows bat批处理基础命令学习教程(转载)
一.基础语法: 1.批处理文件是一个“.bat”结尾的文本文件,这个文件的每一行都是一条DOS命令.可以使用任何文本文件编辑工具创建和修改.2.批处理是一种简单的程序,可以用 if 和 goto 来控 ...
- esayui combotree 只能选择子节点
esayui combotree 只能选择子节点用onBeforeSelect:参数是node,节点被选中之前触发,返回false取消选择动作. 网上找了好多都没一个可用的,要想知道他是子节点还是根节 ...
- 【转】C++ const用法 尽可能使用const
http://www.cnblogs.com/xudong-bupt/p/3509567.html C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不 ...
- [剑指Offer] 16.合并两个排序链表
题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. [思路1]递归 /* struct ListNode { int val; struct Lis ...
- 批处理之SET命令
除了 下面分别介绍: 表示第二个字符到倒数第三个字符的值