index.js 调用 bodyIndex.jsjavascript
var React = require('react'); var ReactDOM = require('react-dom'); import ComponentHeader from './components/header'; // (./)当前目录下 import ComponentFooter from './components/footer'; import BodyIndex from './components/bodyIndex' class Index extends React.Component { componentWillMount(){ //定义你的逻辑 console.log("Index - componentWillMount") } componentDidMount(){ console.log("Index - componentDidMount"); } render() { //能够将组件定义为参数 var component = <ComponentHeader/> return ( <div> {component} {/*利用参数形式调用*/} <BodyIndex userId = {123456} userName = {"propsName"} /> <ComponentFooter/> </div> ); } } ReactDOM.render( <Index/>, document.getElementById('example') ); /* 1. index 中生命周期函数最后调用,在各个组件调用完毕后 componentWillMount componentDidMount */
import React from 'react'; import ReactDOM from 'react-dom'; //export 暴露 export default class BodyIndex extends React.Component{ constructor(){ //类的构造函数 super(); // 调用基类的全部的初始化方法 this.state = { userName :"parray", age:10 }; //初始化赋值 } render(){ //解析类的输出 setTimeout(()=>{ //更改state的语句 this.setState({ userName : "userName changed", age : 20 }) },4000); //4s 刷新 return ( <div> <h2>页面主体内容</h2> <p>{this.state.userName} {this.state.age} {this.props.userId} { this.props.userName}</p> </div> ) } }
在index.js中给 bodyIndex 传值java
{this.props.userId} react
Object.assign()
方法用于将全部可枚举的属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。dom
Object.assign(target, ...sources)
target
目标对象sources
(多个)源对象。
var obj = { a: 1 }; var copy = Object.assign({}, obj); console.log(copy); // { a: 1 }
在子组件 bodychild中修改数据,父组件 bodyIndex马上刷新。只能经过事件形式。event 原生参数函数
//建立事件 changerUserInfo(age){ this.setState({age : age}); };
传入了一个参数 99ui
<input type= "button" value="submit" onClick={this.changerUserInfo.bind(this,99)} /> <BodyChild handleChildValueChange = {this.handleChildValueChange.bind(this)}/>
onChange={this.props.handleChildValueChange}this
当子页面内容变动时当即调用,父页面传过来的 handleChildValueChange 函数spa
import React from 'React'; export default class BodyChild extends React.Component{ render(){ return( <div> {/* 当子页面input onchange 发生改变, 调用传入的handleChildValueChange 函数*/} <p>子页面输入: <input type= "text" onChange={this.props.handleChildValueChange}/> </p> </div> ) } } /* 在父页面中 声明事件 //建立 input 改变事件 handleChildValueChange(event){ this.setState({age : event.target.value}) }; 经过 :event.target.value 获取input的值 */
//建立 input 改变事件 handleChildValueChange(event){ // event 原生参数。 this.setState({age : event.target.value}) //取出子页面当前操做空间的值 }; //event.target 获取子页面控件
console.log(event.target); ==》<input type = "text">code
在子页面调用父页面的 Propscomponent
import React from 'react'; import ReactDOM from 'react-dom'; //引入body子页面 import BodyChild from './BodyChild' //export 暴露 export default class BodyIndex extends React.Component{ constructor(){ //类的构造函数 super(); // 调用基类的全部的初始化方法 this.state = { userName :"parray", age:10 }; //初始化赋值 } //建立事件 changerUserInfo(age){ this.setState({age : age}); }; //建立 input 改变事件 handleChildValueChange(event){ this.setState({age : event.target.value}) }; render(){ //解析类的输出 return ( <div> <h2>页面主体内容</h2> <p>{this.props.userId} { this.props.userName}</p> <p>age:{this.state.age}</p> <input type= "button" value="submit" onClick={this.changerUserInfo.bind(this,99)} /> <BodyChild handleChildValueChange = {this.handleChildValueChange.bind(this)}/> </div> ) } } /* ES6语法关于this绑定的问题 1. <input type= "button" value="submit" onClick={this.changerUserInfo.bind(this)} /> bind(this) 2. <BodyChild/> 完成子页面输入反馈到age上面 3. 定义函数 handleChildValueChange 4. 将函数传入子页面 利用属性 handleChildValueChange 5. 有this 如何调用两个参数 <input type= "button" value="submit" onClick={this.changerUserInfo.bind(this,99)} 一点击就能够将 99传入age */