Skip to main content

React

To use lunex-http in a React project, first install it with your preferred package manager:

Using npm

npm install lunex-http

Using Yarn

yarn add lunex-http

Using pnpm

pnpm add lunex-http

Import and Usage

Import LunexClient in your React components or hooks:

import React, { useEffect, useState } from 'react';
import LunexClient from 'lunex-http';

const client = new LunexClient('https://api.example.com', {
Authorization: 'Bearer YOUR_TOKEN'
});

function UsersList() {
const [users, setUsers] = useState(null);
const [error, setError] = useState(null);

useEffect(() => {
client.getAsync('users')
.then(setUsers)
.catch(err => setError(err.message));
}, []);

if (error) return <div>Error: {error}</div>;
if (!users) return <div>Loading...</div>;

return (
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}

export default UsersList;

Notes

  • lunex-http works seamlessly with React hooks and components.
  • Ensure your bundler supports ES modules.
  • TypeScript support is built-in for full typing in React projects.