Reactive Router+Redux Form講座

17826 ワード

Reactive Router+Redux Form講座


前のプロジェクトのレビュー

  • 1ページで構成され、1つのトップレベルの要素ですべてを動作させる.
  • 李徳思はデータのみ受信して使用する.リドス内にデータを挿入する方法が分かりません.
  • 開始前の状態(現在)

  • ミドルウェアの知識不足
  • 李徳思の使い方がよくわからなかった.
  • 開発環境の設定


    gitボイラプロジェクトのインポート


    git clone https://github.com/StephenGrider/ReduxSimpleStarter.git
    cd ReduxSimpleStarter
    npm i
    npm start

    postmanのインストール


    postmanサーバリクエストを容易にするためのプログラム
    使用時にリクエストurlを入力してsendを行うと、受信したデータ型がわかります.送信したデータ型を指定することもできます.

    バックエンド・サーバに代わるアプリケーション


    http://reduxblog.herokuapp.com

    URLリクエストに基づいてコンポーネントのreact-router-domを選択するのに役立ちます


    react-routerとの違いは、Domへのアクセスを支援することです.
    npm install --save [email protected]

    axios、redux-promiseをインストールして、情報の保存とサーバとの通信を支援


    npm i --save axios redux-promise

    アップグレードされたコンソール?


    https://stephengrider.github.io/JSPlaygrounds/
    const posts = [
      {id:4,title:"Hi"},
      {id:25,title:"Bye"},
      {id:36,title:"Gavri"},
      ]
    posts 
    _.mapKeys(posts,'id')
    const state = _.mapKeys(posts,'id')
    state
    state["4"]
    サイトがこのコードを作成している場合は、

    これらの結果が得られます
    ここで重要なのはlodashのmapkeys関数ですこの関数は、同じ名前のデータ(最初のパラメータから受信した配列から2番目のパラメータに渡される)を新しいキー値の既存のオブジェクトの値として指定することによって、新しいオブジェクトを作成します.
    Ex)posts= {"id": 4, "title": "Hi"} .mapKeys(posts,"id") -> {"4"(New Key):{"id": 4, "title": "Hi"}(New Value)}

    Router Switchを選択

     <BrowserRouter basename="hi">
          <div>
           	<Switch>
              <Route path="/posts/new" component={PostsNew} />
              <Route path="/" component={PostsIndex} />
            </Switch>
          </div>
        </BrowserRouter>
    Switch 컴포넌트를 사용하면 어떤일이 벌어질까?スイッチドアのように該当する箱に落ちている./posts/new要求された場合、既存のコンポーネントPostsNewとPostsIndexの両方がロードされます.
    スイッチを使用すると、pathの条件に合った場所に落ちます.
    ここで、ルータの位置を上下に/posts/newに変更すると、リクエストもまずpath="/"に遭遇するので、PostsIndexだけをロードすると悪い結果になります.
    <Switch>
      	<Route path="/" component={PostsIndex} />
      	<Route path="/posts/new" component={PostsNew} />
    </Switch>

    リンクを使用してSPAを保持

    <Link className="btn btn-primary" to="/posts/new">Add a Post</Link>
    to=パスを書くとページはSPAのままになります.

    redux-fomrを使用して値伝達エラーを解決

    reducer index.js 
    import { reducer as formReducer } from "redux-form";// 추가
    const rootReducer = combineReducers({
      posts: PostsReducer,
      form: formReducer,
    });//해당 formReducer 리듀서 추가 
    
    ./posts_new.js
    import { Field, reduxForm } from "redux-form"; //추가
    
    
    <form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
            <Field
              label="Title For Post"
              name="title"
              component={this.renderField}
            ></Field>
      </form>
    
      renderField(field) {
        return (
          <div className="form-group">
            <label>{field.label}</label>
            <input //onChange={field.input.onChange}
              className="form-control"
              type="text"
              {...field.input}
            />
            {field.meta.error}
          </div>
        );
      }
    
    function validate(values) {
      const errors = {};
    
      if (!values.title) {
        errors.title = "Enter a title!";
      }
      if (!values.categories) {
        errors.categories = "Enter a categories!";
      }
      if (!values.content) {
        errors.content = "Enter a content!";
      }
      return errors;
    }
    
    export default reduxForm({
      validate,
      form: "PostsNewForm",
    })(PostsNew);
    
    「ソース」を参照して、Redux-Form Reducerを追加してください.
    Action.type

    授業の途中で道に迷った!😫


    追いついたが、途中で道に迷った.
    実際のプロジェクトを行う過程で、繰り返し熟知して、二度と会わないでください.

    サスペンス


    history.push("/")


    onSubmit(values) {
    this.props.createPost(values, () => {
    this.props.history.push("/");
    });
    }
    ドメインはハッシュpushのパラメータ値に従って移動します
    TAGと関係があるみたい?リンクラベルはどのように移動しますか?
    Link
    export function Link(
    ...params: Parameters
    ): ReturnType;
    export interface Link
    extends React.ForwardRefExoticComponent<
    React.PropsWithoutRef & React.RefAttributes
    > {}
    H.LocationState
    export type LocationState = History.LocationState;
    理解できない...後で正式な書類を見ましょう.
    --History Mdnを添付
    解決)08-23/https://developer.mozilla.org/ko/docs/Web/API/History

    途中でミスに対処する


    bundle.js:22665 Warning: Please use require("history").createBrowserHistory instead of require("history/createBrowserHistory") . Support for the latter will be removed in the next major release.


    Google翻訳:
    使用require("history/createBrowserHistory")大臣require("history").createBrowserHistory後者のサポートは、次の主要リリースから削除されます.
    エラーを確認し、Google翻訳機->の次の主要リリースから削除->更新すればいいんですよね?解決npm i react-router-dom->