项目作者: abraztsov

项目描述 :
Simple React fetch high order component 🚴🏽
高级语言: JavaScript
项目地址: git://github.com/abraztsov/react-fetch-hoc.git
创建时间: 2017-08-26T18:46:51Z
项目社区:https://github.com/abraztsov/react-fetch-hoc

开源协议:MIT License

下载


React Fetch HOC

npm version

An experiment to quickly integrate and use an API requests with High Order Component 🚴🏽

The API might change any day.

Do not use in Production!

How to install ?

npm i -s react-simple-fetch-hoc

What is it ?

It’s a simple library build on React High Order Components and on fetch (currently)
which assumes responsibility for receiving data from the server and transferring it to your component.

Example

  1. import React from 'react';
  2. import fetch from 'isomorphic-fetch';
  3. import fetchHoc from 'react-fetch-hoc';
  4. const API_ROUTE = 'https://jsonplaceholder.typicode.com/posts';
  5. const Post = ({ title, body }) => {
  6. return (
  7. <div>
  8. <h3>{title}</h3>
  9. <div>{body}</div>
  10. </div>
  11. )
  12. }
  13. const withFetchPost = fetchHoc(props => fetch(`${API_ROUTE}${props.id}`));
  14. const PostContainer = withFetchPost(Post);
  15. const App = ({ id = 1 }) => <PostContainer id={id} ></PostContainer>
  16. export default App;

The API get call to https://jsonplaceholder.typicode.com/posts/1 will send us response as

  1. {
  2. title: 'Summer',
  3. body: 'That summer was so fine that I dec...'
  4. }

What problem does it solve ?

Basically everyone is familiar with using fetch/axios in the componentDidMount method. This solution is not the cleanest of all. The concept built into this library allows us keep our components clean and just re-use them where necessary, changing only the shell.

FAQ ?

Feature Requests/Find Bug ?

If you see the potential in this library let’s talk. Search for existing GitHub issues and join the conversation or create new!

Can I use this in production?

I wouldn’t. Many use cases are not be considered yet. If you find some use cases this lib can’t handle yet, please file an issue.