Skip to content

Instantly share code, notes, and snippets.

@Sylvance
Created January 10, 2023 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sylvance/009d5b008c6ca62eeeefcac3f6a44c43 to your computer and use it in GitHub Desktop.
Save Sylvance/009d5b008c6ca62eeeefcac3f6a44c43 to your computer and use it in GitHub Desktop.
Grpc web client
import React, { useEffect, useState } from 'react';
import { createClient } from 'grpc-web-client';
import { DataService } from './generated/data_pb_service';
const DataClient = createClient(DataService);
function DataFetching({ url }) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
async function fetchData() {
setLoading(true);
setError(null);
try {
const request = new GetDataRequest();
request.setUrl(url);
const response = await DataClient.getData(request);
setData(response.getData());
} catch (e) {
setError(e);
} finally {
setLoading(false);
}
}
fetchData();
}, [url]);
return (
<div>
{loading && <p>Loading...</p>}
{error && <p>Error: {error.message}</p>}
{data && <p>Data: {data}</p>}
</div>
);
}
export default DataFetching;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment