/* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0 and the Server Side Public License, v 1; you may not use this file except * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ import React, { useCallback } from 'react'; import { useState } from 'react'; import { EuiText, EuiButton, EuiLoadingSpinner, EuiFieldText, EuiCallOut, EuiFormRow, } from '@elastic/eui'; import { HttpFetchError } from '../../../src/core/public'; import { isError } from './is_error'; import { Services } from './services'; interface Props { getMessageById: Services['getMessageById']; } export function GetMessageRouteExample({ getMessageById }: Props) { const [error, setError] = useState(); const [isFetching, setIsFetching] = useState(false); const [message, setMessage] = useState(''); const [id, setId] = useState(''); const doFetch = useCallback(async () => { if (isFetching) return; setIsFetching(true); const response = await getMessageById(id); if (isError(response)) { setError(response); setMessage(''); } else { setError(undefined); setMessage(response); } setIsFetching(false); }, [isFetching, getMessageById, setMessage, id]); return (

GET example with param

This examples uses a simple GET route that takes an id as a param in the route path.

setId(e.target.value)} data-test-subj="routingExampleGetMessageId" /> doFetch()} > {isFetching ? : 'Get message'} {error !== undefined ? ( {error.message} ) : null} {message !== '' ? (

Message is:

{message}

) : null}
); }