React專案開發你需要知道哪些? react專案開發具體事項(附實例)
本篇文章主要的講述了關於react專案開發需要注意的哪些情況,想知道的同學就趕緊點進來看吧,現在就一起來閱讀本篇文章吧
对react项目开发,我大概对它的知识点进行了分类,主要由以下几个部分组成。
基礎寫法
#入口頁面的寫法
import React,{ Component } from 'react';import { render } from 'react-dom';import Main from './components/Main'; render(<Main />,document.getElementById('app'));
元件的寫法
import React,{ Component } from 'react'; export default class Main extends Component{ render(){ return ( <p> 组件 </p> ) } }
元件傳值
#父元件傳給子元件
父组件传给子组件靠props
import React,{ Component } from 'react'; import { render } from 'react-dom';class Main extends Component{ render(){ return ( <p> <Child title="我是父组件"/> </p> ) } }class Child extends Component{ render(){ return( <h2>{this.props.title}</h2> ) } } render(<Main />,document.getElementById('app'));
子元件傳給父元件
子组件传给父组件靠事件 子组件传递给父组件的过程描述,父组件传递给子组件一个函数,子组件执行这个函数,然后把子组件自己的值可以通过穿参的方式传递进去。然后父组件接受到这个值,进行相应的操作。
import React,{ Component } from 'react'; import { render } from 'react-dom';class Main extends Component{ constructor(props){ super(props); this.state = { value:'init value' } } render(){ return ( <p> <p>{this.state.value}</p> <Child onClick={(value)=>{this.setState({value:value})}}/> </p> ) } }class Child extends Component{ render(){ return( <button onClick={()=>this.props.onClick("子组件的值")}>点击传值</button> ) } } render(<Main />,document.getElementById('app'));
webpack
webpack的配置一般分为这么几个部分,entry、output、plugin、devServer、module等。 entry告诉webpack入口在哪。 output告诉webpack将来把文件打包到哪。 plugin代表一些其他的操作,如打开浏览器、文件压缩等处理。 devServer代表开发的时候启动一个本地服务器 module代表各种loader用来解析你的代码。
一個普通的webpack設定檔如下:
var webpack = require('webpack');module.exports = { entry:"./src/index.js", output:{ path:'public', filename:'bundle.js' }, devServer:{ historyApiFallback:true, hot:true, inline:true }, plugins:[ new webpack.DefinePlugin({ 'process.env.NODE.ENV': "development" }), new webpack.HotModuleReplacementPlugin(), new OpenBrowserPlugin({ url: 'http://localhost:8080' }) ], module:{ loaders:[{ test:/\.js[x]?$/, exclude:/node_modules/, loader:'babel-loader', query:{ presets:['es2015','react','stage-1'] } },{ test:/\.css$/, loaders:['style',css] },{ test:/\.(png|jpg)$/, loader:"url-loader" }] } }
es6部分
在react 中,es6語法一般是要會一些的,針對react,關於es6一些基本的使用以及寫法,這裡列出來。
import和export
import引入一個東西
import webpack from 'webpack';import React from 'react';import { Component } from 'react';
其中使用「{}」引入的變數是那個檔案中必須存在的變數名稱相同的變數。
不使用「{}」引入的變數是那個檔案中export default預設拋出的變量,其中變數名稱可以不一樣。
export拋出一個東西。
function a(){ console.log(1); }let b = 1;export a;export b;export default a;
其中export可以拋出多個,多次使用。
export default只能使用一個,表示預設拋出。
class和extends
class的本質是一個申明類別的關鍵字。它存在的意義和var、let、const、function等都是一樣的。
使用方式:
class Main{}
extends代表繼承,使用方式:
class Main extends Component{}
constructor代表建構函數,super是從父類別繼承屬性和方法。
class Main extends Component{ constructor(props){ super(props) } }
生命週期函數
基本生命週期函數
分成三個狀態
- ##Mounting
- Updating
- Unmounting
- #Mounting階段–一般在這個階段生命週期函數只會執行一次
constructor()
componentWillMount()
componentDidMount()
render() - Updating階段–會執行多次
componentWillReceiveProps()
shouldComponentUpdate()
render()
componentDidUpdate() - Unmountint階段–組件卸載期
componentWillUnmount()
這就是現階段的組件生命週期函數。之前還有兩個生命週期函數叫 getDefaultProps 以及 getInitialState。
但是它們的函數現在被constructor代替。
- constructor
常見的使用方式就是state的初始化
constructor(props){ super(props); this.state = { value:'' }}
登入後複製登入後複製 - componentWillMount
進行一些初始化的操作。或進行一些資料載入。
componentWillMount(){ <br/> this.fetchData(); <br/>} <br/><br/>
- #componentDidMount
常見場景就是資料請求
componentWillMount(){ this.fetchData(); }
登入後複製登入後複製 - # render
一個react元件中必須包含的函數,傳回jsx語法的dom元素
render(){ return ( <p>123</p> ) }
登入後複製登入後複製 - componentWillReceiveProps
在props傳遞的時候,可以在render之前進行一些處理。不會觸發二次setState。
只有一個參數。代表的是props物件 - shouldComponentUpdate
有兩個參數,分別代表props和state
必須傳回一個true或false。否則會語法報錯。
在進行一些效能最佳化的時候非常有用 - componentDidUpdate
元件載入完畢,進行某些操作
- componentWillUnmount
最常見的場景,將元件附加的setInterval、setTimeout進行清除。
componentWillUnMount(){ <br/> clearInterval(this.timer); <br/>} <br/><br/>
常见的使用场景是,根据传递不同的props,渲染不同的界面数据。
项目比较复杂的情况下,一个页面的值发生变化,就要导致另一个页面的值发生改变,这样需要通过props的方式来告知对方,你的值发生改变。让另外一个组件更新dom。需要使用这个周期函数进行监听接受到的props,从而根据这个props进行相应的数据处理。
登入後複製登入後複製
shouldComponentUpdate解析常见的使用场景是,根据传递不同的props,渲染不同的界面数据。 项目比较复杂的情况下,一个页面的值发生变化,就要导致另一个页面的值发生改变,这样需要通过props的方式来告知对方,你的值发生改变。让另外一个组件更新dom。需要使用这个周期函数进行监听接受到的props,从而根据这个props进行相应的数据处理。
这个函数的返回值是一个布尔值。返回一个true。
返回一个false的情况下,它的的状态不会进行更新。
登入後複製登入後複製
效能最佳化部分这个函数的返回值是一个布尔值。返回一个true。 返回一个false的情况下,它的的状态不会进行更新。
immutable.js
登入後複製登入後複製
資料請求部分ajax在react中,可以使用傳統的XMLHttpRequest物件進行資料請求。 immutable.js
var xhr = new XMLHttpRequest();
xhr.open(type, url, true);
xhr.onReadyStateChange = ()=>{
if (xhr.readyState == 4 && xhr.status == 200) {
sucess(xhr.responseText);
}
}
基本的使用方式是:Promise物件
const Promise = require(`../vendor/bluebird/bluebird.js`);let get = (url,data) => { return new Promise((resolve, reject) => { if(res){ resolve(res); }else if(err){ reject(err); } })}
fetch("http://homework.shsoapp.com:80/ttzyservice/task/getTaskSubjectList",{ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, mode: 'cors', body: "page=1&rows=10" }).then(res =>{ console.log(res); return res.json() }).then(res => { console.log(res); })
import { BrowserRouter as Router, Link, Route,Switch} from 'react-router-dom'; export default class Main extends Component{ render(){ return( <Router> <p> <Switch> <Route path='/' component={page1}> <Route path='/home' component={page2}> <Route path='/age' component={page3}> </Switch> </p> </Router> ) } }
const ADD_TASK = "ADD_TASK";const ADD_CONTENT = "ADD_CONTENT"; export function addtask(task){ return { type: ADD_TASK, task } } export function addContent(content){ return { type: ADD_CONTENT, content } }
import { addtask,addContent } from 'actions';export function(state = '',action){ switch (action.type){ case ADD_TASK: return action.task; break; case ADD_CONTENT: return action.content; break; default: return state; } }
对react项目开发,我大概对它的知识点进行了分类,主要由以下几个部分组成。
import React,{ Component } from 'react';import { render } from 'react-dom';import Main from './components/Main';
render(<Main />,document.getElementById('app'));
登入後複製登入後複製
元件的寫法import React,{ Component } from 'react';import { render } from 'react-dom';import Main from './components/Main'; render(<Main />,document.getElementById('app'));
import React,{ Component } from 'react';
export default class Main extends Component{
render(){ return (
<p>
组件
</p>
)
}
}
登入後複製
元件傳值父元件傳給子元件import React,{ Component } from 'react'; export default class Main extends Component{ render(){ return ( <p> 组件 </p> ) } }
父组件传给子组件靠props
登入後複製登入後複製import React,{ Component } from 'react';
import { render } from 'react-dom';class Main extends Component{
render(){ return (
<p>
<Child title="我是父组件"/>
</p>
)
}
}class Child extends Component{
render(){ return(
<h2>{this.props.title}</h2>
)
}
}
render(<Main />,document.getElementById('app'));
登入後複製登入後複製
子元件傳給父元件父组件传给子组件靠props
import React,{ Component } from 'react'; import { render } from 'react-dom';class Main extends Component{ render(){ return ( <p> <Child title="我是父组件"/> </p> ) } }class Child extends Component{ render(){ return( <h2>{this.props.title}</h2> ) } } render(<Main />,document.getElementById('app'));
子组件传给父组件靠事件
子组件传递给父组件的过程描述,父组件传递给子组件一个函数,子组件执行这个函数,然后把子组件自己的值可以通过穿参的方式传递进去。然后父组件接受到这个值,进行相应的操作。
登入後複製登入後複製import React,{ Component } from 'react';
import { render } from 'react-dom';class Main extends Component{
constructor(props){ super(props); this.state = {
value:'init value'
}
}
render(){ return (
<p>
<p>{this.state.value}</p>
<Child onClick={(value)=>{this.setState({value:value})}}/>
</p>
)
}
}class Child extends Component{
render(){ return(
<button onClick={()=>this.props.onClick("子组件的值")}>点击传值</button>
)
}
}
render(<Main />,document.getElementById('app'));
登入後複製登入後複製
webpack子组件传给父组件靠事件 子组件传递给父组件的过程描述,父组件传递给子组件一个函数,子组件执行这个函数,然后把子组件自己的值可以通过穿参的方式传递进去。然后父组件接受到这个值,进行相应的操作。
import React,{ Component } from 'react'; import { render } from 'react-dom';class Main extends Component{ constructor(props){ super(props); this.state = { value:'init value' } } render(){ return ( <p> <p>{this.state.value}</p> <Child onClick={(value)=>{this.setState({value:value})}}/> </p> ) } }class Child extends Component{ render(){ return( <button onClick={()=>this.props.onClick("子组件的值")}>点击传值</button> ) } } render(<Main />,document.getElementById('app'));
webpack的配置一般分为这么几个部分,entry、output、plugin、devServer、module等。
entry告诉webpack入口在哪。
output告诉webpack将来把文件打包到哪。
plugin代表一些其他的操作,如打开浏览器、文件压缩等处理。
devServer代表开发的时候启动一个本地服务器
module代表各种loader用来解析你的代码。
登入後複製登入後複製
一個普通的webpack設定檔如下:webpack的配置一般分为这么几个部分,entry、output、plugin、devServer、module等。 entry告诉webpack入口在哪。 output告诉webpack将来把文件打包到哪。 plugin代表一些其他的操作,如打开浏览器、文件压缩等处理。 devServer代表开发的时候启动一个本地服务器 module代表各种loader用来解析你的代码。
var webpack = require('webpack');module.exports = { entry:"./src/index.js", output:{ path:'public', filename:'bundle.js' }, devServer:{ historyApiFallback:true, hot:true, inline:true }, plugins:[ new webpack.DefinePlugin({ 'process.env.NODE.ENV': "development" }), new webpack.HotModuleReplacementPlugin(), new OpenBrowserPlugin({ url: 'http://localhost:8080' }) ], module:{ loaders:[{ test:/\.js[x]?$/, exclude:/node_modules/, loader:'babel-loader', query:{ presets:['es2015','react','stage-1'] } },{ test:/\.css$/, loaders:['style',css] },{ test:/\.(png|jpg)$/, loader:"url-loader" }] } }
import webpack from 'webpack';import React from 'react';import { Component } from 'react';
其中使用“{}”引入的变量是那个文件中必须存在的变量名相同的变量。
不使用“{}”引入的变量是那个文件中export default默认抛出的变量,其中变量名可以不一样。
export抛出一个东西。
function a(){ console.log(1); }let b = 1;export a;export b;export default a;
其中export可以抛出多个,多次使用。
export default只能使用一个,表示默认抛出。
class和extends
class的本质是一个申明类的关键字。它存在的意义和var、let、const、function等都是一样的。
使用方式:
class Main{}
extends代表继承,使用方式:
class Main extends Component{}
constructor代表构造函数,super是从父类继承属性和方法。
class Main extends Component{ constructor(props){ super(props) } }
生命周期函数
基本生命周期函数
分三个状态
Mounting
Updating
Unmounting
Mounting阶段–一般在这个阶段生命周期函数只会执行一次
constructor()
componentWillMount()
componentDidMount()
render()Updating阶段–会执行多次
componentWillReceiveProps()
shouldComponentUpdate()
render()
componentDidUpdate()Unmountint阶段–组件卸载期
componentWillUnmount()
这就是现阶段的组件生命周期函数。之前还有两个生命周期函数叫 getDefaultProps 以及 getInitialState。
但是它们的功能现在被constructor代替。
组件的生命周期使用场景
constructor
常见的一个使用方式就是state的初始化constructor(props){ super(props); this.state = { value:'' }}
登入後複製登入後複製componentWillMount
进行一些初始化的操作。或者进行一些数据加载。<br/>componentWillMount(){ <br/> this.fetchData(); <br/>} <br/>
componentDidMount
常见场景就是数据请求componentWillMount(){ this.fetchData(); }
登入後複製登入後複製render
一个react组件中必须包含的函数,返回jsx语法的dom元素render(){ return ( <p>123</p> ) }
登入後複製登入後複製componentWillReceiveProps
在props传递的时候,可以在render之前进行一些处理。不会触发二次setState。
只有一个参数。代表的是props对象shouldComponentUpdate
有两个参数,分别代表props和state
必须返回一个true或者false。否则会语法报错。
在进行一些性能优化的时候非常有用componentDidUpdate
组件加载完毕,进行某些操作componentWillUnmount
最常见的场景,对组件附加的setInterval、setTimeout进行清除。<br/>componentWillUnMount(){ <br/> clearInterval(this.timer); <br/>} <br/>
componentWillReceiveProps解析
常见的使用场景是,根据传递不同的props,渲染不同的界面数据。 项目比较复杂的情况下,一个页面的值发生变化,就要导致另一个页面的值发生改变,这样需要通过props的方式来告知对方,你的值发生改变。让另外一个组件更新dom。需要使用这个周期函数进行监听接受到的props,从而根据这个props进行相应的数据处理。
shouldComponentUpdate解析
这个函数的返回值是一个布尔值。返回一个true。 返回一个false的情况下,它的的状态不会进行更新。
性能优化部分
immutable.js
数据请求部分
ajax
在react中,可以使用传统的XMLHttpRequest对象进行数据请求。
var xhr = new XMLHttpRequest();
xhr.open(type, url, true);
xhr.onReadyStateChange = ()=>{
if (xhr.readyState == 4 && xhr.status == 200) {
sucess(xhr.responseText);
}
}
promise
promise是es6提出的数据请求方式。目前很多浏览器还没有实现。但是有promise的polyfill,如blueBird.js
基本的使用方式是:Promise对象
const Promise = require(`../vendor/bluebird/bluebird.js`);let get = (url,data) => { return new Promise((resolve, reject) => { if(res){ resolve(res); }else if(err){ reject(err); } })}
fetch
fetch的基本使用方式:
fetch("http://homework.shsoapp.com:80/ttzyservice/task/getTaskSubjectList",{ method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, mode: 'cors', body: "page=1&rows=10" }).then(res =>{ console.log(res); return res.json() }).then(res => { console.log(res); })
react 路由
基本使用
import { BrowserRouter as Router, Link, Route,Switch} from 'react-router-dom'; export default class Main extends Component{ render(){ return( <Router> <p> <Switch> <Route path='/' component={page1}> <Route path='/home' component={page2}> <Route path='/age' component={page3}> </Switch> </p> </Router> ) } }
redux的简单实用
actions
const ADD_TASK = "ADD_TASK";const ADD_CONTENT = "ADD_CONTENT"; export function addtask(task){ return { type: ADD_TASK, task } } export function addContent(content){ return { type: ADD_CONTENT, content } }
reducers
import { addtask,addContent } from 'actions';export function(state = '',action){ switch (action.type){ case ADD_TASK: return action.task; break; case ADD_CONTENT: return action.content; break; default: return state; } }
以上是React專案開發你需要知道哪些? react專案開發具體事項(附實例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

React前後端分離指南:如何實現前後端的解耦和獨立部署,需要具體程式碼範例在當今的Web開發環境中,前後端分離已經成為一種趨勢。透過將前端和後端程式碼分開,可以讓開發工作更加靈活、高效,並且方便進行團隊協作。本文將介紹如何使用React實現前後端分離,從而實現解耦和獨立部署的目標。首先,我們要先理解什麼是前後端分離。傳統的Web開發模式中,前端和後端是耦合在

如何利用React和Flask建構簡單易用的網路應用引言:隨著網路的發展,網路應用的需求也越來越多樣化和複雜化。為了滿足使用者對於易用性和效能的要求,使用現代化的技術堆疊來建立網路應用變得越來越重要。 React和Flask是兩個在前端和後端開發中非常受歡迎的框架,它們可以很好的結合在一起,用來建立簡單易用的網路應用。本文將詳細介紹如何利用React和Flask

如何利用React和RabbitMQ建立可靠的訊息傳遞應用程式引言:現代化的應用程式需要支援可靠的訊息傳遞,以實現即時更新和資料同步等功能。 React是一種流行的JavaScript庫,用於建立使用者介面,而RabbitMQ是一種可靠的訊息傳遞中間件。本文將介紹如何結合React和RabbitMQ建立可靠的訊息傳遞應用,並提供具體的程式碼範例。 RabbitMQ概述:

ReactRouter使用指南:如何實現前端路由控制隨著單頁應用的流行,前端路由成為了一個不可忽視的重要部分。 ReactRouter作為React生態系統中最受歡迎的路由庫,提供了豐富的功能和易用的API,使得前端路由的實作變得非常簡單和靈活。本文將介紹ReactRouter的使用方法,並提供一些具體的程式碼範例。安裝ReactRouter首先,我們需要

如何利用React和ApacheKafka來建立即時資料處理應用介紹:隨著大數據與即時資料處理的興起,建構即時資料處理應用成為了許多開發者的追求。 React作為一個流行的前端框架,與ApacheKafka作為一個高效能的分散式訊息系統的結合,可以幫助我們建立即時資料處理應用。本文將介紹如何利用React和ApacheKafka建構即時資料處理應用,並

PHP、Vue和React:如何選擇最適合的前端框架?隨著互聯網技術的不斷發展,前端框架在Web開發中起著至關重要的作用。 PHP、Vue和React作為三種代表性的前端框架,每一種都具有其獨特的特徵和優勢。在選擇使用哪種前端框架時,開發人員需要根據專案需求、團隊技能和個人偏好做出明智的決策。本文將透過比較PHP、Vue和React這三種前端框架的特徵和使

Java框架與React框架的整合:步驟:設定後端Java框架。建立專案結構。配置建置工具。建立React應用程式。編寫RESTAPI端點。配置通訊機制。實戰案例(SpringBoot+React):Java程式碼:定義RESTfulAPI控制器。 React程式碼:取得並顯示API回傳的資料。

如何利用React開發一個響應式的後台管理系統隨著互聯網的快速發展,越來越多的企業和組織需要一個高效、靈活、易於管理的後台管理系統來處理日常的操作事務。 React作為目前最受歡迎的JavaScript庫之一,提供了一種簡潔、高效和可維護的方式來建立使用者介面。本文將介紹如何利用React開發一個響應式的後台管理系統,並給出具體的程式碼範例。建立React專案首先
