react 項目構建
(github項目)壓縮包:https://pan.baidu.com/s/1_5LxosvcJCIvGkpt0_yrqg密碼:hkkk
1.找到react項目壓縮包解壓
可進入項目目錄,修改項目信息:
使用visual studio code打開項目目錄
查看項目目錄結構:
查看包管理源鏡像路徑是否爲國內
查看轉譯器預設支持配置:.babelrc
查看項目依賴包:package.json(默認已npm init初始化了)
熱加載依賴也在開發者依賴庫安裝:
安裝項目依賴包:npm install
項目支持github版本管理(修改repository元素即可)
格式如下:
支持本地webserver開發環境
配置文件:
本地監聽端口也可以設置代理服務器配置
開啓熱加載
啓動項目:
訪問webserver:
修改index.js然後掛載在id爲root的DOM元素
對應掛載的index.html的Dom元素
驗證:
編輯index.js,增加子元素
import React from 'react';
import ReactDom from 'react-dom'
//要增加的子元素
class SubEle extends React.Component{
render(){
return <div>Sub content</div>
//return <div>{1>2?"ture":"false"}</div>
}
}
//定義React組件(JSX語法)
class Root extends React.Component {
render () {
return (
<div>
<h2>My first test</h2>
<hr/>
<SubEle />
</div>);
}
}
//使用render渲染函數把React組件關聯至Dom的Element元素
ReactDom.render(<Root />,document.getElementById('root'))
// //另類語法
// class Root extends React.Component {
// render () {
// return React.createElement('div',null,'My first test')
// }
// }
// ReactDom.render(React.createElement(Root),document.getElementById('root'))
瀏覽器驗證:
動態渲染:
import React from 'react';
import ReactDom from 'react-dom'
//要增加的子元素
class SubEle extends React.Component{
state = {
p1:'mozilla',
p2:'.com'
}
render(){
//this.state.p1 = 'www.mozilla'
setTimeout(() => this.setState({p1:'http://mozilla'}),3000) //超過3秒重繪內容
return <div>{this.state.p1 + this.state.p2}</div>
// return <div>{1>2?"ture":"false"}</div>
}
}
//定義React組件(JSX語法)
class Root extends React.Component {
render () {
return (
<div>
<h2>My first test</h2>
<hr/>
<SubEle />
</div>);
}
}
//使用render渲染函數把React組件關聯至Dom的Element元素
ReactDom.render(<Root />,document.getElementById('root'))
查看瀏覽器變化:
設置鼠標點擊事件觸發回調函數產生告警:
import React from 'react';
import ReactDom from 'react-dom'
//要增加的子元素
class SubEle extends React.Component{
handleClick(event){
let x = event.target
alert("觸發的元素id是:"+ x.id)
}
render(){
return (
<div id="t1" onClick={this.handleClick.bind(this)}>
點擊這句話,會觸發一個事件,並彈出一個警示框
</div>
)
}
}
//定義React組件(JSX語法)
class Root extends React.Component {
render () {
return (
<div>
<h2>My first test</h2>
<hr/>
<SubEle />
</div>);
}
}
//使用render渲染函數把React組件關聯至Dom的Element元素
ReactDom.render(<Root />,document.getElementById('root'))
瀏覽器訪問:
上例子對應的html格式:
<html>
<head>
<script type="text/javascript">
function getEventTrigger(event) {
x = event.target;
alert("觸發的元素id是:"+ x.id)
}
</script>
</head>
<body>
<div id="t1" onmousedown="getEventTrigger(event)">
點擊這句話,會觸發一個事件,並彈出一個警示框
</div>
hello world !!!
</body>
</html>
查看瀏覽器訪問:
改進:
import React from 'react';
import ReactDom from 'react-dom'
//要增加的子元素
class Toggle extends React.Component{
state = {flag:true}
handleClick(event){
console.log(event.target)
console.log(this)
console.log(this.state)
let x = event.target
alert("觸發的元素id是:"+ x.id)
this.setState({flag:!this.state.flag})
}
render(){
let text = this.state.flag?"true":"false"
return (
<div id="t1" onClick={this.handleClick.bind(this)}>
點擊這句話,會觸發一個事件,並彈出一個警示框
flag = {text}
</div>
)
}
}
//定義React組件(JSX語法)
class Root extends React.Component {
render () {
return (
<div>
<h2>My first test</h2>
<hr/>
<Toggle />
</div>);
}
}
//使用render渲染函數把React組件關聯至Dom的Element元素
ReactDom.render(<Root />,document.getElementById('root'))
查看瀏覽器變化:
改進:
import React from 'react';
import ReactDom from 'react-dom'
//要增加的子元素
class Toggle extends React.Component{
constructor(props){
super(props)
this.state = {flag:true}
}
handleClick(event){
let x = event.target
alert("觸發的元素id是:"+ x.id)
this.setState({flag:!this.state.flag})
}
render(){
let text = this.state.flag?"true":"false"
return (
<div id="t1" onClick={this.handleClick.bind(this)}>
點擊這句話,會觸發一個事件,並彈出一個警示框
flag = {text}
<hr />
{this.props.var1}
<hr />
{this.props.var2.student}
<hr />
{this.props.children}
</div>);
}
}
//定義React組件(JSX語法)
class Root extends React.Component {
student = 'yzx'
render () {
return (
<div>
<h2>My first test</h2>
<hr/>
<Toggle var1='abc' var2={this}>
<div>我是toggle的子元素</div>
</Toggle>
</div>);
}
}
//使用render渲染函數把React組件關聯至Dom的Element元素
ReactDom.render(<Root />,document.getElementById('root'))
查看瀏覽器變化:
案例:
import React from 'react';
import ReactDom from 'react-dom'
class Sub extends React.Component{
constructor(props){
console.log('Sub constructor')
super(props)
this.state = {count:0}
}
hanlerClink(event){
this.setState({count:this.state.count+1})
}
render(){
console.log('Sub render')
return (
<div id='Sub' onClick={this.hanlerClink.bind(this)}>
sub's count = {this.state.count}
</div>
)
}
componentWillMount(){
//constructor之後,第一次render之前
console.log('Sub componentwillMount')
}
componentDidMount(){
//第一次render之後
console.log('Sub componentDitMount')
}
componentWillUnmount(){
//清理工作
console.log('Sub componentWillUnmount')
}
}
class Root extends React.Component{
constructor(props){
console.log('Root constructor')
super(props)
this.state = {}
}
render(){
return (
<div>
<Sub />
</div>
)
}
}
ReactDom.render(<Root />,document.getElementById('root'))
查看瀏覽器變化:
案例:
import React from 'react';
import ReactDom from 'react-dom'
class Sub extends React.Component{
constructor(props){
console.log('Sub constructor')
super(props)
this.state = {count:0}
}
handlerClink(event){
console.log(event)
this.setState({count:this.state.count+1})
}
render(){
console.log('Sub render')
return (
<div id='Sub' onClick={this.handlerClink.bind(this)}>
sub's count = {this.state.count}
</div>
)
}
componentWillMount(){
//constructor之後,第一次render之前
console.log('Sub componentwillMount')
}
componentDidMount(){
//第一次render之後
console.log('Sub componentDitMount')
}
componentWillUnmount(){
//清理工作
console.log('Sub componentWillUnmount')
}
componentWillReceiveProps(nextProps){
//props變更時,接到新的props了,交給shouldComponentUpdate
console.log(this.props)
console.log(nextProps)
console.log('Sub componentWillReceiveProps',this.state.count)
}
shouldComponentUpdate(nextPros,nextState){
console.log('Sub shouldComponentUpdate',this.state.count,nextState)
return true //return false將攔截更新
}
componentWillUpdate(nextPros,nextState){
//同意更新後,真正更新前,之後調用render
console.log('Sb componentWillUpdate',this.state.count,nextState)
}
componentDidUpdate(prevProps,prevState){
//同意更新後,真正更新前,在render之後調用
console.log('Sub componentDidUpdate',this.state.count,prevState)
}
}
class Root extends React.Component{
constructor(props){
console.log('Root constructor')
super(props)
this.state = {flag:true,name:'root',count:0}
}
handlerClink(event){
this.setState({
flag:!this.state.flag,
name:this.state.flag?this.state.name.toLowerCase():this.state.name.toUpperCase(),
count:this.state.count+1
})
}
render(){
return (
<div id='root' onClick={this.handlerClink.bind(this)}>
My Name is {this.state.name}
<br />
root's count = {this.state.count}
<hr />
<Sub parent={this}/> {/*父組件中的點擊會引起子組件的刷新但不觸發子組件的點擊事件。而子組件的點擊會引起父組件的點擊事件 */}
</div>
)
}
}
ReactDom.render(<Root />,document.getElementById('root'))
查看瀏覽器變化:
案例:
import React from 'react';
import ReactDom from 'react-dom'
//無狀態react組件
// function Root(props){
// return (
// <div id='root'>
// My Name is {props.name}
// </div>)
// }
//上面改造成如下:
let Root = props => <div id='root'>My Name is {props.name}</div>
ReactDom.render(<Root name='yzx' />,document.getElementById('root'))
查看瀏覽器變化: