溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

redux-form(V7.4.2)筆記(三之補(bǔ)充)使用Flow初步

發(fā)布時(shí)間:2020-06-23 20:29:24 來源:網(wǎng)絡(luò) 閱讀:693 作者:googlingman 欄目:web開發(fā)

本文是上一篇的簡(jiǎn)短補(bǔ)充——再細(xì)致總結(jié)一下在redux-form項(xiàng)目中使用Flow的步驟。

注:我使用的React開發(fā)工具是WebStorm,所以有些細(xì)節(jié)是有關(guān)于它的。

內(nèi)容如下:

第一步: 初始化項(xiàng)目

一種方法是在你的項(xiàng)目根目錄的用命令列工具輸入下面的指令:

flow init

這將會(huì)創(chuàng)建一個(gè).flowconfig文檔,如果這個(gè)配置文件已經(jīng)存在就不需要再進(jìn)行初始化,這個(gè)配置文檔可以加入自定義的設(shè)置值的,請(qǐng)參考官方文檔中的“Advanced Configuration”里的說明,目前有很多項(xiàng)目里面都已經(jīng)內(nèi)附這個(gè)設(shè)置檔,例如一些React的項(xiàng)目。

另一種方法是不需要使用上面的命令行方式——只需要在WebStorm的配置界面下配置一下即可讓系統(tǒng)自動(dòng)在項(xiàng)目根目錄下生成上面的配置文件。界面如下:

redux-form(V7.4.2)筆記(三之補(bǔ)充)使用Flow初步

【前提】已經(jīng)順利安裝了Flow。
經(jīng)過上面操作后,配置文件自動(dòng)生成,我的內(nèi)容如下:

[ignore]

[include]

[libs]

[lints]

[options]

[strict]

內(nèi)容為空,不錯(cuò),簡(jiǎn)單情況下,讓它為空即可。

第二步: 在代碼文檔中加入要作類型檢查的注釋

一般都在代碼檔案的最上面一行加入,不添加如下標(biāo)志行,則Flow工具是不會(huì)進(jìn)行檢查的。有兩種格式都可以:

// @flow

/ @flow /

第三步: 進(jìn)行檢查

目前支持Flow工具插件的代碼編輯工具很多,常見的Atom, Visual Studio Code(VSC), Sublime與WebStorm都有,當(dāng)有安裝搭配代碼編輯工具的插件時(shí),編輯工具會(huì)輔助顯示檢查的訊息。不過有時(shí)候會(huì)有點(diǎn)卡頓的要等一下,因?yàn)闄z查速度還不是那么快。

或是直接用下面的命令列指令來進(jìn)行檢查亦可:

flow check

簡(jiǎn)單代碼參考

最后,給出我在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;
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI