Hook 是 React 16.8 的新增特性。它可以讓你在不編寫 class 的情況下使用 state 以及其他的 React 特性。
useState
useState 通過在函數(shù)組件里調(diào)用它來給組件添加一些內(nèi)部 state。React 會(huì)在重復(fù)渲染時(shí)保留這個(gè) state。useState 會(huì)返回一對(duì)值:當(dāng)前狀態(tài)和一個(gè)讓你更新它的函數(shù),你可以在事件處理函數(shù)中或其他一些地方調(diào)用這個(gè)函數(shù)。它類似 class 組件的 this.setState,但是它不會(huì)把新的 state 和舊的 state 進(jìn)行合并。
接下來通過一個(gè)示例來看看怎么使用 useState。
有這么一個(gè)需求:需要在 iframe 中加載外部網(wǎng)頁(yè)。
初始的代碼我們通過 函數(shù)式組件 來實(shí)現(xiàn)這個(gè)需求,只需要簡(jiǎn)單的渲染一個(gè) iframe:
import React, { useState } from 'react';
import styles from './index.less';
function Link(props) {
const { match: { params: { link = '' } = {} } = {} } = props;
const enCodeUrl = decodeURIComponent(link);
const url = enCodeUrl.startsWith('http') ? enCodeUrl : `http://${enCodeUrl}`;
return (
<React.Fragment>
<iframe
title={link}
src={url}
style={{ width: '100%', height: '100%', verticalAlign: 'top' }}
frameBorder="0"
/>
</React.Fragment>
);
}
export default Link;
這樣,我們就完成需求了;
新的需求來了,我們需要給頁(yè)面添加一個(gè) loading 效果,實(shí)現(xiàn)的方式很簡(jiǎn)單,監(jiān)聽 iframe 的 load 事件 來設(shè)置loading的開始和結(jié)束。
為了實(shí)現(xiàn)這個(gè)需求,我們需要存放loading的狀態(tài),而函數(shù)式組件是沒有自有狀態(tài)的,我們得改造成 class 組件:
import React from 'react';
import { Spin } from 'antd';
import styles from './index.less';
export default class Link extends React.Component {
state = {
// 存放loading狀態(tài)
iLoading: true,
};
linkLoad() {
// 更新loading
this.setState({ iLoading: false });
}
render() {
const { match: { params: { link = '' } = {} } = {} } = this.props;
const { iLoading } = this.state;
const enCodeUrl = decodeURIComponent(link);
const url = enCodeUrl.startsWith('http') ? enCodeUrl : `http://${enCodeUrl}`;
return (
<React.Fragment>
<Spin spinning={iLoading} wrapperClassName={styles['iframe-loading']}>
<iframe
onLoad={this.linkLoad.bind(this)}
title={link}
src={url}
style={{ width: '100%', height: '100%', verticalAlign: 'top' }}
frameBorder="0"
/>
</Spin>
</React.Fragment>
);
}
}
為了實(shí)現(xiàn)一個(gè)頁(yè)面的loading,我們需要去使用class,同時(shí)還需要bind綁定this等繁瑣行為,這只是一個(gè)簡(jiǎn)單的需求,而我們卻可以通過hooks來解決這些問題,同時(shí)還能解決組件間狀態(tài)復(fù)用的問題,我們使用useState來實(shí)現(xiàn)。
- 導(dǎo)入 useState
import React, { useState } from 'react';
- 定義狀態(tài)
// useState 的參數(shù)為狀態(tài)初始值,setInitLoading為變更狀態(tài)值的方法
const [initLoading, setInitLoading] = useState(true);
- 更新狀態(tài)
onLoad={() => setInitLoading(false)}
完整代碼如下:
import React, { useState } from 'react';
import { Spin } from 'hzero-ui';
import styles from './index.less';
function Link(props) {
const { match: { params: { link = '' } = {} } = {} } = props;
const [initLoading, setInitLoading] = useState(true);
const enCodeUrl = decodeURIComponent(link);
const url = enCodeUrl.startsWith('http') ? enCodeUrl : `http://${enCodeUrl}`;
return (
<React.Fragment>
<Spin spinning={initLoading} wrapperClassName={styles['iframe-loading']}>
<iframe
onLoad={() => setInitLoading(false)}
title={link}
src={url}
style={{ width: '100%', height: '100%', verticalAlign: 'top' }}
frameBorder="0"
/>
</Spin>
</React.Fragment>
);
}
export default Link;
使用 useState 使得我們的代碼更加輕便,同時(shí)更容易理解和閱讀。