0.0.4 • Published 3 years ago

@lsky/http-react v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

✨ Features

  • Extends Axios
  • React Component
  • Support <HttpProvider />

📦 installing

npm

$ npm install @lsky/http-react --save

yarn

$ yarn add @lsky/http-react

And you need to install Axios and React

$ yarn add react axios

🔨 Example

import React from 'react'
import HttpReact, { Post, Get, HttpProvider } from '@lsky/http-react'

class Index extends React.Component {
  render() {
    return (
      <div>
        <HttpReact
          url="/test/test"
          data={{ param: 'abc' }}
          method="post"
          onResponse={(response) => { console.log('response: ', response) }}
          onError={(error) => console.log('error: ', error)}
          onLoading={(loading) => console.log('isLoading: ', loading)}
        >
          http react
        </HttpReact>
        <Post
          url="/test/test"
          data={{ param: 'abc' }}
          onResponse={(response) => { console.log('post: ', response) }}
        >
          post http react
        </Post>
        <Get
          url="/test/test"
          params={{ param: 'abc' }}
          onResponse={(response) => { console.log('get: ', response) }}
        >
          post http react
        </Get>

        <HttpProvider baseURL={"http://localhost:3000"}>
          <Post
            url="/test/test"
            data={{ path: '/test/test' }}
            onResponse={(response) => { console.log('post: ', response) }}
          >
            post http react
          </Post>
        </HttpProvider>
        <Get 
          url={get.url} 
          onResponse={(response) => { console.log('get: ', response) }} 
          loading={<div>loading...</div>} 
        />
      </div>
    )
  }
}

⚡️ WebSocket

import React from 'react'
import { WebSocket } from '@lsky/http-react'

const sendMsg = (socket) => {
  socket.send('message test')
}
const onMessage = (messageEvent) => {
  console.log('message: ', messageEvent)
}

class Index extends React.PureComponent {
  render() {
    return (
      <WebSocket
        url="ws://localhost:4000"
        onMessage={onMessage}
      >
        {({ socket }) => <div><button onClick={() => sendMsg(socket)}>send msg</button></div>}
      </WebSocket>
    )
  }
}

🍰 Components & PropTypes

HttpReact

attrtypedefault valuedesc
methodstringnullhttp method
urlstringnullurl
debouncenumber500debounce
datastring | plain object | URLSearchParamsnulldata is the data to be sent as the request body; Only applicable for request methods 'PUT', 'POST', and 'PATCH'
paramsplain objectnullparams are the URL parameters to be sent with the request
childrenReact.ReactChildnullreact node
loadingReact.ReactNode | booleannullshow in loading
onResponse(response) => voidnullonResponse
onError(error) => voidnullonError
onLoading(isLoading: boolean) => voidnullonLoading

Get

attrtypedefault valuedesc
urlstringnullurl
debouncenumber500debounce
paramsplain objectnullparams are the URL parameters to be sent with the request
childrenReact.ReactChildnullreact node
loadingReact.ReactNode | booleannullshow in loading
onResponse(response) => voidnullonResponse
onError(error) => voidnullonError
onLoading(isLoading: boolean) => voidnullonLoading

Post

attrtypedefault valuedesc
urlstringnullurl
debouncenumber500debounce
datastring | plain object | URLSearchParamsnulldata is the data to be sent as the request body; Only applicable for request methods 'PUT', 'POST', and 'PATCH'
paramsplain objectnullparams are the URL parameters to be sent with the request
childrenReact.ReactChildnullreact node
loadingReact.ReactNode | booleannullshow in loading
onResponse(response) => voidnullonResponse
onError(error) => voidnullonError
onLoading(isLoading: boolean) => voidnullonLoading

WebSocket

attrtypedefault valuedesc
urlstringnullwebsocket url
timeoutnumber1000restart time
maxnumber10restart count
childrenReact.ReactNode | ({socket}) => React.ReactNodenullreact node
onOpen(event) => voidnullsocket open callback
onMessage(messageEvent) => voidnullrecieve message callback
onClose(closeEvent) => voidnullsocket close callback
onError(event) => voidnullsocket error callback