javascript 从Medium Article重构的重构分支方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 从Medium Article重构的重构分支方法相关的知识,希望对你有一定的参考价值。

import React from 'react';
import { withState, pure, branch, renderComponent, compose } from 'recompose';
import { gql, graphql } from 'react-apollo';

// Define a very basic loading state component - you could make this
// a nice animation or something
const Loading = () => (
  <div>Loading</div>
);

// Define an HoC that displays the Loading component instead of the
// wrapped component when props.data.loading is true
const displayLoadingState = branch(
  (props) => props.data.loading,
  renderComponent(Loading),
);

// The pure component that renders UI
const BookSearchResultsPure = ({ data: { allBooks } }) => (
  <ul className="bookName">
    {allBooks.map(book =>
      <li key={book.id}>
          {book.title} by {book.author[0].name}
      </li>
    )}
  </ul>
);

const data = graphql(gql`
 query BookSearchQuery($filter: BookFilter) {
        allBooks(filter: $filter) {
            id
            title
            author {
                id
                name
            }
        }
    }
 `, {
  // note: this variables object is the same object as in Graphiql so if it works
  // there, copy/paste that object here. Query Variables Winodw in Graphiql = the
  // value of the variable key below.
  options: ({ filter }) => ({ variables: { filter: {title_contains:filter }} }),
});

// Put everything together!
const BookSearchResults = compose(
  data,
  displayLoadingState,
  pure,
)(BookSearchResultsPure);

// Use recompose to keep the state of the input so that we
// can use functional component syntax
// See: <https://github.com/acdlite/recompose/blob/master/docs/API.md#withstate>
const filter = withState('filter', 'setFilter',  '');

const BookSearchPure = ({ filter, setFilter }) => (
  <div className="bookList">
    <input
      type="text"
      placeholder="Search Book Here"
      value={ filter }
      onChange={(e) => setFilter(e.target.value)}
    />

    <BookSearchResults filter={filter} />
  </div>
);

// Attach the state to the pure component
const BookSearch = compose(
  filter,
  pure,
)(BookSearchPure);

export default BookSearch;

以上是关于javascript 从Medium Article重构的重构分支方法的主要内容,如果未能解决你的问题,请参考以下文章

javascript medium gist import AuthLoading2.js

javascript lcom-techblog:LTI JavaScript示例(https://medium.com/lcom-techblog/simple-lti-tool-consumer-

markdown MEDIUM BLOG POST - Javascript的3个主要范例:面向对象编程的三个原则[第2部分,共4部分]

如何在 JavaScript 中让代码更加精简

如何在javascript中解析和格式化不规则CSV中的字符串?

javascript 一个简单的CSS调试器。在此处了解更多信息:https://medium.freecodecamp.org/88529aa5a6a3。要使用,请在https://zaydek.g