yarn add create-react-app
当前使用的版本(3.2.0)
npx create-react-app my-react
cd my-react
yarn start
知识点测试时都是修改 App.js 文件中的内容
import React from ‘react‘; function App() { return ( ...修改的入口参考位置 ) } export default App;
使用 jsx 语法,jsx: javascript xml,变成js需要编译转换
<h1>hello world</h1>
<p>hello</p> <p>world</p>
<label> <span>name:</span> <input/> </label>
<input type="text"/>
<input type={‘text‘}/>
const msg = ‘hello world‘; ...... <h1>{msg}</h1>
<div> <span>1</span> {/*<span>111</span>*/} </div>
jsx 中属性关键字和js中关键字冲突(class、for),使用其他的词代替
<div> <label htmlFor={‘hd‘}>name:</label> <input className={‘name‘} id={‘hd‘}/> </div>
<p>{123}</p>
<div> <p>1 + 1 = {1 + 1}</p> <p>1 > 2 is {(1 > 2).toString()}</p> <p>(5>2)&&(5<8) is {((5 > 2) && (5 < 8)).toString()}</p> </div>
以对象形式来使用,属性的写法使用小驼峰(去掉连接符号)
<div style={{backgroundColor:‘#f00‘}}>11</div>
key保持不变
[‘张三‘, ‘李四‘, ‘王五‘, ‘马六‘]
.map((it, ix) => ({name: it, id: ix}))
.filter(it=>it.id > 0)
.map(it=>(
<div key={it.id}>{it.name}</div>
))
<div> {2 >1 && <h1>hello world</h1>} {2 < 1 && <h1>hello world</h1>} {2 < 1 ? <h1>world</h1> : <h1>hello</h1>} </div>
//getter/setter 函数为了直接使用 this.xxx class Person extends React.Component { constructor(props) { super(props); this._name = ‘master‘; this._age = 100; } get name() { return this._name; } set name(new_name) { this._name = new_name; } get age() { return this._age; } set age(new_age) { this._age = new_age; } render() { return ( <div> <h2>name: {this.name}</h2> <h2>age: {this.age}</h2> </div> ); } }
直接当成一个标签使用
<Person/>
直接使用Teacher组件,Teacher组件中使用了Student组件
class Student extends React.Component { render() { return ( <span>学生</span> ); } } class Teacher extends React.Component { render() { return ( <div> <h1>teacher</h1> { [1, 2, 3, 4, 5] .map(it=>({name:it, id:‘s‘ + it})) .map(it=>( <div key={it.id}> <Student/> {it.name} </div> )) } </div> ); } }
class Person extends React.Component { constructor(props) { super(props); } render() { return ( <div> <h1>name: {this.props.name}</h1> <h1>age: {this.props.age}</h1> </div> ); } } ...... <Person name={‘Mrs Li‘} age={25}/>
//组件 class Item extends React.Component { constructor(props) { super(props); } render() { return ( <p>{this.props.desc}: {this.props.text}</p> ); } } class StudentList extends React.Component { constructor(props) { super(props); } render() { return ( this.props.list.map(item => ( <ul key={item.id}> <li>class: {item.class}</li> <li> <ul> <li> <span>members: </span> { item.members.map(it => ( <span key={it.id}> {it.name} </span> )) } </li> </ul> </li> </ul> )) ); } } class Teacher extends React.Component { constructor(props) { super(props); } render() { return ( <div> <Item text={this.props.name} desc={‘name‘}/> <Item text={this.props.age} desc={‘age‘}/> <Item text={this.props.job} desc={‘job‘}/> <p>students-list</p> <StudentList list={this.props.students}/> </div> ) } } ......(数据) const data = { name: ‘Mrs Li‘, age: 55, job: ‘teacher‘, students: [ { class: ‘031‘, members: [‘zhou‘, ‘fru‘, ‘mike‘] }, { class: ‘037‘, members: [‘张三‘, ‘李四‘, ‘王五‘] }, { class: ‘007‘, members: [‘Bob‘, ‘Alex‘] } ] }; data.students.forEach((item, index) => { item.id = ‘s‘ + index; item.members = item.members.map((it, ix) => ( {id: ‘t‘ + ix, name: it} )); }); ......(使用) <Teacher name={data.name} age={data.age} job={data.job} students={data.students}/>
class Event extends React.Component { click() { console.log(‘clicking‘, window.Date.now()); } render() { return ( <h1 style={{textAlign: ‘center‘}} onClick={this.click.bind(this)}>点击</h1> ); } } ...... <Event/>
很可能会影响效率,不推荐使用
class Count extends React.Component { constructor() { super(); this.val = 0; } click() { ++this.val; console.log(‘click‘, this.val); this.forceUpdate(); } render() { return ( <div style={{textAlign: ‘center‘}}> <p> <span>click times: {this.val}</span> </p> <p> <input onClick={this.click.bind(this)} type=‘button‘ value=‘点击‘/> </p> </div> ); } } ...... <Count/>
class Count extends React.Component { constructor() { super(); this.state = { val: 0 }; } click() { this.setState({ val: this.val + 1 }); } get val() { return this.state.val; } render() { console.log(‘rendering‘, window.Date.now()); return ( <div style={{textAlign: ‘center‘}}> <p> <span>click times: {this.val}</span> </p> <p> <input onClick={this.click.bind(this)} type=‘button‘ value=‘点击‘/> </p> </div> ); } } ...... <Count/>
子组件不能修改父组件参数,参数是复杂对象则可以改其属性
class Child extends React.Component { constructor(props) { super(props); } render() { return ( <p> 子组件: {this.props.value} </p> ); } } class Parent extends React.Component { constructor() { super(); this.state = { value: 0 }; } get value() { return this.state.value; } click() { this.setState({ value: this.value + 1 }); } render() { return ( <div> <p> 父组件: {this.value} </p> <Child value={this.value}/> <p> <input onClick={this.click.bind(this)} value={‘点击‘} type={‘button‘}/> </p> </div> ); } } ...... <Parent/>
const a = <h1> <p style={{background: ‘#f86‘}}> <span>color</span> </p> </h1>; const b = a.props.children; const c = b.props.children; const d = b.props.style; ...... {c} {b} {a} <p>background: {d.background}</p>
处理方法:在 shouldComponentUpdate 中控制更新
class Child extends React.Component { constructor(props) { super(props); this.state = { time: -1 }; this.request(); } shouldComponentUpdate(nextProps, nextState, nextContext) { console.log(nextProps, nextState) return nextProps.val !== this.props.val || this.state.time !== nextState.time; } componentDidUpdate(prevProps, prevState, snapshot) { this.request(); } request() { setTimeout(() => { this.setState({ time: this.props.val }); }, 500); } render() { console.log(‘rendering‘); return ( <div> <p>click times: {this.props.val}</p> <p>time: {this.state.time}</p> </div> ); } } class Parent extends React.Component { constructor(props) { super(props); this.state = { val: 0 }; console.log(‘constructor‘) } componentDidCatch(error, errorInfo) { console.log(‘catch‘) } componentDidMount() { console.log(‘mount‘); } componentDidUpdate(prevProps, prevState, snapshot) { console.log(‘update‘); } componentWillUnmount() { console.log(‘unmount‘); } shouldComponentUpdate(nextProps, nextState, nextContext) { console.log(‘should update‘); // console.log(nextProps, nextState, nextContext) return true; } click() { this.setState({ val: this.state.val + 1 }); } render() { console.log(‘render‘); return ( <div> <Child val={this.state.val}/> <p> <input type={‘button‘} onClick={this.click.bind(this)} value={‘click‘}/> </p> </div> ); } }
使用调用函数的方式传递参数
class Child extends React.Component { constructor(props) { super(props); this.cnt = 0; } clickHandler() { this.props.message(‘msg--‘ + ++this.cnt); } render() { return ( <div> <input value={‘click‘} onClick={this.clickHandler.bind(this)} type={‘button‘}/> </div> ); } } class Parent extends React.Component { constructor() { super(); this.state = { msg: ‘waiting click‘ } } message(msg) { this.setState({ msg }); } render() { return ( <div> <p>message: {this.state.msg}</p> <Child message={this.message.bind(this)}/> </div> ); } } ...... <Parent/>
ref 值重复会覆盖,最有一个有效
组件也同样适用
class Reference extends React.Component { componentDidMount() { console.log(this.refs.test.id); } render() { return ( <div> <input ref={‘test‘} id={123}/> </div> ); } } ...... <Reference/>
class Child extends React.Component { constructor(props) { super(props); this.val = ‘child‘ + this.props.id; } render() { return ( <div> <p>{this.val}</p> </div> ); } } class Parent extends React.Component { componentDidMount() { console.log(this.refs.first_child.val); console.log(this.refs.second_child.props.id); } render() { return ( <div> <Child ref=‘first_child‘ id={1}/> <Child ref=‘second_child‘ id={2}/> </div> ); } } ...... <Parent/>
就是个普通的参数传递
class Child extends React.Component { render() { return ( <div> <p>{this.props.parent.val}</p> </div> ); } } class Parent extends React.Component { constructor() { super(); this.val = ‘parent‘; } render() { return ( <div> <Child parent={this}/> </div> ); } } ...... <Parent/>
class Form extends React.Component { constructor() { super(); this.state = { val: ‘‘ }; } handlerChange(event) { this.setState({ val : event.target.value }); } render() { return ( <div> <p> <input type={‘text‘} onChange={this.handlerChange.bind(this)} value={this.state.val} placeholder={‘请输入名称‘}/> </p> <p> <input defaultValue={‘默认值‘}/> </p> <p> <input type={‘password‘} placeholder={‘请输入密码‘}/> </p> </div> ); } } ...... <Form/>
Created: 2019-11-29 周五 23:11