🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@lsky/http-react

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lsky/http-react

latest
Source
npmnpm
Version
0.0.4
Version published
Maintainers
1
Created
Source

logo

Http React

A http React Component

✨ 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

Keywords

react

FAQs

Package last updated on 11 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts