本文是上一篇的簡(jiǎn)短補(bǔ)充——再細(xì)致總結(jié)一下在redux-form項(xiàng)目中使用Flow的步驟。
注:我使用的React開(kāi)發(fā)工具是WebStorm,所以有些細(xì)節(jié)是有關(guān)于它的。
內(nèi)容如下:
一種方法是在你的項(xiàng)目根目錄的用命令列工具輸入下面的指令:
flow init
這將會(huì)創(chuàng)建一個(gè).flowconfig文檔,如果這個(gè)配置文件已經(jīng)存在就不需要再進(jìn)行初始化,這個(gè)配置文檔可以加入自定義的設(shè)置值的,請(qǐng)參考官方文檔中的“Advanced Configuration”里的說(shuō)明,目前有很多項(xiàng)目里面都已經(jīng)內(nèi)附這個(gè)設(shè)置檔,例如一些React的項(xiàng)目。
另一種方法是不需要使用上面的命令行方式——只需要在WebStorm的配置界面下配置一下即可讓系統(tǒng)自動(dòng)在項(xiàng)目根目錄下生成上面的配置文件。界面如下:
【前提】已經(jīng)順利安裝了Flow。
經(jīng)過(guò)上面操作后,配置文件自動(dòng)生成,我的內(nèi)容如下:
[ignore]
[include]
[libs]
[lints]
[options]
[strict]
內(nèi)容為空,不錯(cuò),簡(jiǎn)單情況下,讓它為空即可。
一般都在代碼檔案的最上面一行加入,不添加如下標(biāo)志行,則Flow工具是不會(huì)進(jìn)行檢查的。有兩種格式都可以:
// @flow
或
/ @flow/
目前支持Flow工具插件的代碼編輯工具很多,常見(jiàn)的Atom, Visual Studio Code(VSC), Sublime與WebStorm都有,當(dāng)有安裝搭配代碼編輯工具的插件時(shí),編輯工具會(huì)輔助顯示檢查的訊息。不過(guò)有時(shí)候會(huì)有點(diǎn)卡頓的要等一下,因?yàn)闄z查速度還不是那么快。
或是直接用下面的命令列指令來(lái)進(jìn)行檢查亦可:
flow check
最后,給出我在redux-form官方給出的示例文件中使用Flow的簡(jiǎn)單方式(尚未作細(xì)致使用;當(dāng)然,官方網(wǎng)站給出的主要是在普通JS項(xiàng)目中使用Flow的更為全面的示例展示):
// @flow
import React from 'react';
import { connect } from 'react-redux';
import { Field, reduxForm, formValueSelector } from 'redux-form';
import type { FormProps } from 'redux-form';
type Props = {
someCustomThing: string
} & FormProps
// ^^^^^^^^^^
let SelectingFormValuesForm = (props:Props) => {
const {
favoriteColorValue,
fullName,
handleSubmit,
hasEmailValue,
pristine,
reset,
submitting,
} = props;
return (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<div>
<Field
name="firstName"
component="input"
type="text"
placeholder="First Name"
/>
</div>
</div>
<div>
<label>Last Name</label>
<div>
<Field
name="lastName"
component="input"
type="text"
placeholder="Last Name"
/>
</div>
</div>
<div>
<label htmlFor="hasEmail">Has Email?</label>
<div>
<Field
name="hasEmail"
id="hasEmail"
component="input"
type="checkbox"
/>
</div>
</div>
{hasEmailValue &&
<div>
<label>Email</label>
<div>
<Field
name="email"
component="input"
type="email"
placeholder="Email"
/>
</div>
</div>}
<div>
<label>Favorite Color</label>
<div>
<Field name="favoriteColor" component="select">
<option />
<option value="#ff0000">Red</option>
<option value="#00ff00">Green</option>
<option value="#0000ff">Blue</option>
</Field>
</div>
</div>
{favoriteColorValue &&
<div
style={{
height: 80,
width: 200,
margin: '10px auto',
backgroundColor: favoriteColorValue,
}}
/>}
<div>
<button type="submit" disabled={pristine || submitting}>
Submit {fullName}
</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>
Clear Values
</button>
</div>
</form>
);
};
// The order of the decoration does not matter.
// Decorate with redux-form
SelectingFormValuesForm = reduxForm({
form: 'selectingFormValues', // a unique identifier for this form
})(SelectingFormValuesForm);
// Decorate with connect to read form values
const selector = formValueSelector('selectingFormValues'); // <-- same as form name
SelectingFormValuesForm = connect(state => {
// can select values individually
const hasEmailValue = selector(state, 'hasEmail');
const favoriteColorValue = selector(state, 'favoriteColor');
// or together as a group
const { firstName, lastName } = selector(state, 'firstName', 'lastName');
return {
hasEmailValue,
favoriteColorValue,
fullName: `${firstName || ''} ${lastName || ''}`,
};
})(SelectingFormValuesForm);
export default SelectingFormValuesForm;
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前名稱(chēng):redux-form(V7.4.2)筆記(三之補(bǔ)充)使用Flow初步-創(chuàng)新互聯(lián)
標(biāo)題來(lái)源:http://newbst.com/article48/dgioep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)、Google、面包屑導(dǎo)航、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容