Created
January 10, 2023 14:18
-
-
Save Sylvance/009d5b008c6ca62eeeefcac3f6a44c43 to your computer and use it in GitHub Desktop.
Grpc web client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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