Write a React app that can take user input, call an api and display the result in HTML.
- User
Write a React app that can take user input, call an api and display the result in HTML.
- Assistant
Here's an example of a React app that takes user input, calls an API, and displays the result in HTML:
import React, { useState } from 'react'; import axios from 'axios';
function App() { const [inputValue, setInputValue] = useState(''); const [result, setResult] = useState('');
const handleInputChange = (event) => { setInputValue(event.target.value); };
const handleButtonClick = async () => { try { const response = await axios.get(
https://example-api.com/${inputValue}); setResult(response.data); } catch (error) { console.error(error); } };return ( Submit {result} ); }
export default App;
This app uses React's useState hook to manage the user input and the API response. When the user types into the input field, the handleInputChange function updates the inputValue state. When the user clicks the button, the handleButtonClick function makes an API request using the axios library and updates the result state with the response data. Finally, the app displays the inputValue, a submit button, and the result in separate div elements.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.