React基础教程
1、安装cnpm install create-react-app -g create-react-app my-app cd my-app/ npm start 在浏览器中打开 http://localhost:3000/ ,就可以看到默认的界面了~ 2、JSX
const ele = <img src={user.acatarUrl} /> JSX特性更接近js而不是html。React Dom 使用小驼峰命名,比如 const ele = ( <h1 className ="greet"> Hello,React! </h1> ) 3、元素渲染元素是 <div id="root"></div> <script> const ele = <h1>hello world!</h1>; ReactDOM.render(ele,document.getElementById("root")) </script> 渲染结果: <div id="root"> <h1>hello world!</h1> </div> 4、父->子通信
import React,{ Component } from ‘react‘; class Child extends Component { render(){ return <h1> This is Child: {this.props.name} </h1> } } export default Child;
import Child from ‘.Child‘; class App extends Component { render() { return ( <div className="App"> <Child name="heimayu" /> </div> ); } } export default App; 父组件通过 5、状态状态(
举例:封装一个简易钟表组件 class Time extends Component { constructor(props){ super(props); // 初始化定义 this.state = { date: this.getNowTime(new Date()) } } // 组件渲染dom后 componentDidMount(){ this.timer = setInterval(()=>{ this.setState({ date: this.getNowTime(new Date()) }) },1000) } // 获取2位数的时间 getTwoNumber(time){ return time.toString().padStart(2,‘0‘); } // 获取当前时间 getNowTime(date){ let y = date.getFullYear(); let M = this.getTwoNumber(date.getMonth() + 1); let d = this.getTwoNumber(date.getDate()); let h = this.getTwoNumber(date.getHours()); let m = this.getTwoNumber(date.getMinutes()); let s = this.getTwoNumber(date.getSeconds()); return `${y}年${M}月${d}日 ${h}时${m}分${s}秒` } render(){ return ( <h2>现在时间是:{this.state.date}</h2> ) } } export default Time; 2个知识点:
constructor(props){ super(props); // 初始化定义 this.state = { date: this.getNowTime(new Date()) } }
{this.state.date}
this.setState((prevState,props)=>{ prevState 表示上一个状态 props 表示当前组件的属性对象 })
6、事件处理注意事项: class Test3 extends Component { constructor(props) { super(props); this.state = { info: "myinfo:" } // 事件处理可以在这里定义,也可以像下面一样直接在事件的表达式里定义~ // this.clickEvt = this.clickEvt.bind(this,"heimayu","18"); } render() { return ( <h3> 事件处理: <button onClick={this.clickEvt.bind(this,"18")}>点击</button> </h3> ) } clickEvt(name,age,e) { console.log(this.state.info); console.log(name); console.log(age); e.preventDefault(); } } 7、条件渲染和JS语法一样,可以使用 class App extends Component { constructor(props) { super(props); // 定义一个状态 this.state = { showTextNum: 1 } } render() { // 条件渲染 let TextJSX; if (this.state.showTextNum == 1) { TextJSX = <Text1 /> } else { TextJSX = <Text2 /> } return ( <div className="App"> <button onClick={this.changeState.bind(this)}>切换</button> // 这里使用大括号 {TextJSX} </div> ); } changeState() { this.setState((prevState) => { return { showTextNum: prevState.showTextNum == 1 ? 2 : 1 } }) } } 上面的例子还可以使用三元表达式进行简写: return ( <div className="App"> <button onClick={this.changeState.bind(this)}>切换</button> { this.state.showTextNum == 1 ? <Text1 /> : <Text2 /> } </div> ); 8、列表渲染通过对数据进行处理,返回对应的渲染出来的JSX模板,例如下例中,使用 render() { const listItems = [{ id:1,name: "heimayu" },{ id: 2,name: "黑玛鱼" }] return ( <div className="App"> // 简单的列表渲染 <ul> { listItems.map(item =>{ return ( <li key={item.id}>{item.name}</li> ) }) } </ul> </div> ); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |