新聞中心
這篇文章將為大家詳細(xì)講解有關(guān)react hooks的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
10余年的江川網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整江川建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“江川網(wǎng)站設(shè)計”,“江川網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
React在16.8版本正式發(fā)布了Hooks。關(guān)注了很久,最近正好有一個小需求,趕緊來試一下。
需求描述
需求很簡單,部門內(nèi)部的一個數(shù)據(jù)查詢小工具。大致長成下面這樣:
用戶首次訪問頁面,會拉取數(shù)據(jù)展示。輸入篩選條件,點(diǎn)擊查詢后,會再次拉取數(shù)據(jù)在前端展示。
需求實(shí)現(xiàn)
使用React Class Component的寫法
如果使用以前的class寫法,簡單寫一下,代碼可能大概長成下面這樣:
import React from 'react'; import { Tabs, Input, RangeTime, Button, Table } from './components'; class App extends React.Component { ... state = { type: [], id: '', title: '', date: [], dataList: [] } componentDidMount() { this.fetchData(); } render() {
使用React Hooks的寫法
關(guān)于React hooks的相關(guān)內(nèi)容,這里就不贅述了。可以直接查看react官方文檔,寫得非常好。
https://reactjs.org/docs/hooks-intro.html
本次需求其實(shí)就兩個邏輯:1、輸入篩選項(xiàng) 。2、查詢數(shù)據(jù)
主頁面一個hooks,處理篩選項(xiàng)以及數(shù)據(jù)展示。數(shù)據(jù)請求邏輯單獨(dú)弄一個hooks。
主頁面hooks:
import React, { useState, useEffect} from 'react'; import { Tabs, Input, RangeTime, Button, Table } from './components'; const App = () => { // 數(shù)據(jù)類型 const tabs = [{ key: 1, value: '類型1' }, { key: 0, value: '類型2' }]; const [tab, setTab] = useState(1); // 數(shù)據(jù)ID const [dataId, setDataid] = useState(''); // 標(biāo)題 const [title, setTitle] = useState(''); // 時間區(qū)間, 默認(rèn)為至今一周時間 const now = Date.now(); const [timeRange, setTimeRange] = useState([now - 1000 * 60 * 60 * 24 * 7, now]); // 數(shù)據(jù)列表 const [dataList, setDataList] = useState([]); // 點(diǎn)擊搜索按鈕 function handleBtnClick() { // 請求數(shù)據(jù) ... } return}; ID 標(biāo)題
上面的代碼,完成了篩選項(xiàng)的處理邏輯。下面來實(shí)現(xiàn)負(fù)責(zé)數(shù)據(jù)請求的hooks.
數(shù)據(jù)請求hooks:
import React, { useState, useEffect } from 'react'; import jsonp from '../tools/jsonp'; function MyFecth(url) { // 是否正在請求中 const [isLoading, setIsLoanding] = useState(false); // 請求參數(shù) const [queryParams, setQueryParams] = useState(null); // 請求結(jié)果 const [data, setData] = useState(null); // 向接口發(fā)起請求 const fetchData = async () => { if(queryParams === null) { return; } setIsLoanding(true); const res = await jsonp({ url: url, data: queryParams }); setData(res); setIsLoanding(false); } // 只要queryParams改變,就發(fā)起請求 useEffect(()=> { fetchData(); }, [queryParams]); // 供外部調(diào)用 const doGet = (params) => { setQueryParams(params); } return { isLoading, data, doGet } } export default MyFecth;
在主頁面中,引用數(shù)據(jù)請求hooks:
import React, { useState, useEffect} from 'react'; import { Tabs, Input, RangeTime, Button, Table } from './components'; import MyFecth from './MyFetch'; const App = () => { // ①使用數(shù)據(jù)請求hooks const { isLoading, data, doGet } = MyFecth('http://xxx'); // 數(shù)據(jù)類型 const tabs = [{ key: 1, value: '類型1' }, { key: 0, value: '類型2' }]; const [tab, setTab] = useState(1); // 數(shù)據(jù)ID const [dataId, setDataid] = useState(''); // 標(biāo)題 const [title, setTitle] = useState(''); // 時間區(qū)間, 默認(rèn)為至今一周時間 const now = Date.now(); const [timeRange, setTimeRange] = useState([now - 1000 * 60 * 60 * 24 * 7, now]); // 數(shù)據(jù)列表 const [dataList, setDataList] = useState([]); // 點(diǎn)擊搜索按鈕 function handleBtnClick() { // ②點(diǎn)擊按鈕后請求數(shù)據(jù) const params = {}; title && (params.title = title); dataId && (params.dataId = dataId); params.startTime = String(timeRange[0]); params.endTime = String(timeRange[1]); doGet(params); } // ③data改變后,重新渲染列表。 // 這里相當(dāng)于 componentDidUpdate。當(dāng)data發(fā)生改變時,重新渲染頁面 useEffect(() => { setDataList(data); }, [data]); // ④首次進(jìn)入頁面時,無任何篩選項(xiàng)。拉取數(shù)據(jù),渲染頁面。 // useEffect第二個參數(shù)為一個空數(shù)組,相當(dāng)于在 componentDidMount 時執(zhí)行該「副作用」 useEffect(() => { doGet({}); }, []); return}; ID 標(biāo)題
關(guān)于React Hooks的一些思考
使用hooks寫完這個需求,最直觀的感受就是,代碼寫起來很爽。不需要像以前那樣寫很多的setState。其次就是
hooks的api設(shè)計得很優(yōu)秀,一個useState的就能將【狀態(tài)】和【變更狀態(tài)的邏輯】兩兩配對。React的基本思想就是【數(shù)據(jù)到視圖的映射】,在hooks中,使用useEffect來表明其中的【副作用】,感覺react官方也傾向于不區(qū)分componentDidMount和componentDidUpdate。
從api設(shè)計就能看出,hooks提倡組件狀態(tài)細(xì)粒度地拆分。在一個hooks組件中,可能包含很多的狀態(tài),如果用戶的某些操作,需要同時修改兩個狀態(tài),那么我需要分別調(diào)用這兩個狀態(tài)的修改邏輯,這樣會導(dǎo)致組件被重新render兩次。而在使用class寫法的組件中,只需要一次setState就好。這樣看來,hooks中render兩次的操作,可能會帶來些許的性能問題 ? 這就要求我們在設(shè)計組件結(jié)構(gòu)和state時,多斟酌,多抽象。
關(guān)于“react hooks的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
文章名稱:reacthooks的示例分析
URL地址:http://www.ef60e0e.cn/article/jieipi.html