Some code samples for this pupose:

  1. import React from "react";
  2. import MyDemo from "./mydemo.jsx";
  3.  
  4. export default class Square extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. count: 0
  9. };
  10. this.handleChange = this.handleChange.bind(this);
  11. this.changeMyStateFromChild = this.changeMyStateFromChild.bind(this);
  12. }
  13. componentDidMount() {
  14. let me = this;
  15. me.setState({
  16. count: me.state.count + 1
  17. });
  18. console.log(me.state.count); // 打印出0
  19. me.setState({
  20. count: me.state.count + 1
  21. });
  22. console.log(me.state.count); // 打印出0
  23. setTimeout(function() {
  24. me.setState({
  25. count: me.state.count + 1
  26. });
  27. console.log(me.state.count); // 打印出2
  28. }, 0);
  29. setTimeout(function() {
  30. me.setState({
  31. count: me.state.count + 1
  32. });
  33. console.log(me.state.count); // 打印出3
  34. }, 0);
  35. }
  36. handleChange(e) {
  37. let me = this;
  38. const target = e.target;
  39. console.log(me);
  40. alert(me.state.count);
  41. this.setState({
  42. [target.name]: target.value
  43. });
  44. console.log(MyDemo);
  45. }
  46. changeMyStateFromChild(state) {
  47. // this.setState(state);
  48. debugger;
  49. alert(state);
  50. }
  51. render() {
  52. return (
  53. <div onChange={e => this.handleChange(e)}>
  54. <MyDemo
  55. title={this.state.count}
  56. changeParent={this.changeMyStateFromChild}
  57. />
  58. <input type="text" name="username" />
  59. <input type="text" name="password" />
  60. <button onClick={() => alert(MyDemo.title)}>click </button>
  61. <h1>{this.state.count}</h1>
  62. </div>
  63. );
  64. }
  65. }

React module methods with passing props to child or invoking callback to parent.的更多相关文章

  1. React 深入系列3:Props 和 State

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...

  2. React组件的state和props

    React组件的state和props React的数据是自顶向下单向流动的,即从父组件到子组件中,组件的数据存储在props和state中.实际上在任何应用中,数据都是必不可少的,我们需要直接的改变 ...

  3. React LifeCycle Methods & re-learning 2019

    React LifeCycle Methods & re-learning 2019 v16.9.0 https://reactjs.org/docs/react-component.html ...

  4. React组件三大属性之 props

    React组件三大属性之 props 理解1) 每个组件对象都会有props(properties的简写)属性2) 组件标签的所有属性都保存在props中 作用1) 通过标签属性从组件外向组件内传递变 ...

  5. React报错之Parameter 'props' implicitly has an 'any' type

    正文从这开始~ 总览 当我们没有为函数组件或者类组件的props声明类型,或忘记为React安装类型声明文件时,会产生"Parameter 'props' implicitly has an ...

  6. React中的State与Props

    一.State 1.什么是 state 一个组件的显示形态可以由数据状态和外部参数决定,其中,数据状态为 state,外部参数为 props 2.state 的使用 组件初始化时,通过 this.st ...

  7. React 的组件与 this.props对象

    1.组件 React 允许将代码封装成组件,然后像插入普通 HTML 标签一样,在网页中插入这个组件.React.createClass 的方法就是用于生成一个组件类. 2.this.props对象 ...

  8. [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 ...

  9. React.js 小书 Lesson22 - props.children 和容器类组件

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson22 转载请注明出处,保留原文链接和作者信息. 有一类组件,充当了容器的作用,它定义了一种外层 ...

随机推荐

  1. MyEclipse - 问题集 - 创建Maven项目,JDK版本默认是1.5

    修改Maven的配置文件settings.xml,增加profile节点,如下所示: <profile> <id>jdk-1.8</id> <activati ...

  2. 机器学习实战一:kNN手写识别系统

    实战一:kNN手写识别系统 本文将一步步地构造使用K-近邻分类器的手写识别系统.由于能力有限,这里构造的系统只能识别0-9.需要识别的数字已经使用图形处理软件,处理成具有相同的色彩和大小:32像素*3 ...

  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 ...

  4. 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 ...

  5. 并查集——poj2492(带权并查集入门)

    一.题目回顾 题目链接:传送门 题意:给定n只虫子,不同性别的可以在一起,相同性别的不能在一起.给你m对虫子,判断中间有没有同性别在一起的. 二.解题思路 种类并查集 和poj1073的本质一样 详见 ...

  6. windows bat批处理基础命令学习教程(转载)

    一.基础语法: 1.批处理文件是一个“.bat”结尾的文本文件,这个文件的每一行都是一条DOS命令.可以使用任何文本文件编辑工具创建和修改.2.批处理是一种简单的程序,可以用 if 和 goto 来控 ...

  7. esayui combotree 只能选择子节点

    esayui combotree 只能选择子节点用onBeforeSelect:参数是node,节点被选中之前触发,返回false取消选择动作. 网上找了好多都没一个可用的,要想知道他是子节点还是根节 ...

  8. 【转】C++ const用法 尽可能使用const

    http://www.cnblogs.com/xudong-bupt/p/3509567.html C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不 ...

  9. [剑指Offer] 16.合并两个排序链表

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. [思路1]递归 /* struct ListNode { int val; struct Lis ...

  10. 批处理之SET命令

    除了 下面分别介绍: 表示第二个字符到倒数第三个字符的值