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
进行一些初始化的操作。或者进行一些数据加载。<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项目开发,我大概对它的知识点进行了分类,主要由以下几个部分组成。
基础写法
入口页面的写法
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
进行一些初始化的操作。或者进行一些数据加载。<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和WebSocket构建实时聊天应用引言:随着互联网的快速发展,实时通讯越来越受到人们的关注。实时聊天应用已经成为现代社交和工作生活中不可或缺的一部分。本文将介绍如何利用React和WebSocket构建一个简单的实时聊天应用,并提供具体的代码示例。一、技术准备在开始构建实时聊天应用之前,我们需要准备以下技术和工具:React:一个用于构建

React前后端分离指南:如何实现前后端的解耦和独立部署,需要具体代码示例在当今的Web开发环境中,前后端分离已经成为一种趋势。通过将前端和后端代码分开,可以使得开发工作更加灵活、高效,并且方便进行团队协作。本文将介绍如何使用React实现前后端分离,从而实现解耦和独立部署的目标。首先,我们需要理解什么是前后端分离。传统的Web开发模式中,前端和后端是耦合在

如何利用React和Flask构建简单易用的网络应用引言:随着互联网的发展,网络应用的需求也越来越多样化和复杂化。为了满足用户对于易用性和性能的要求,使用现代化的技术栈来构建网络应用变得越来越重要。React和Flask是两种在前端和后端开发中非常受欢迎的框架,它们可以很好的结合在一起,用来构建简单易用的网络应用。本文将详细介绍如何利用React和Flask

如何利用React和RabbitMQ构建可靠的消息传递应用引言:现代化的应用程序需要支持可靠的消息传递,以实现实时更新和数据同步等功能。React是一种流行的JavaScript库,用于构建用户界面,而RabbitMQ是一种可靠的消息传递中间件。本文将介绍如何结合React和RabbitMQ构建可靠的消息传递应用,并提供具体的代码示例。RabbitMQ概述:

React代码调试指南:如何快速定位和解决前端bug引言:在开发React应用程序时,经常会遇到各种各样的bug,这些bug可能使应用程序崩溃或导致不正确的行为。因此,掌握调试技巧是每个React开发者必备的能力。本文将介绍一些定位和解决前端bug的实用技巧,并提供具体的代码示例,帮助读者快速定位和解决React应用程序中的bug。一、调试工具的选择:在Re

ReactRouter使用指南:如何实现前端路由控制随着单页应用的流行,前端路由成为了一个不可忽视的重要部分。ReactRouter作为React生态系统中最受欢迎的路由库,提供了丰富的功能和易用的API,使得前端路由的实现变得非常简单和灵活。本文将介绍ReactRouter的使用方法,并提供一些具体的代码示例。安装ReactRouter首先,我们需

如何利用React和GoogleBigQuery构建快速的数据分析应用引言:在当今信息爆炸的时代,数据分析已经成为了各个行业中不可或缺的环节。而其中,构建快速、高效的数据分析应用则成为了许多企业和个人追求的目标。本文将介绍如何利用React和GoogleBigQuery结合起来构建快速的数据分析应用,并提供详细的代码示例。一、概述React是一个用于构建

如何利用React和ApacheKafka构建实时数据处理应用引言:随着大数据与实时数据处理的兴起,构建实时数据处理应用成为了很多开发者的追求。React作为一个流行的前端框架,与ApacheKafka作为一个高性能的分布式消息传递系统的结合,可以帮助我们搭建实时数据处理应用。本文将介绍如何利用React和ApacheKafka构建实时数据处理应用,并
