Languages
[Edit]
EN

React - button with AJAX request

2 points
Created by:
Root-ssh
175020

Below example shows buton that sends some GET request on mouse click action.

// ONLINE-RUNNER:browser;

//Note: Uncomment import lines during working with JSX Compiler.
// import React from "react";
// import ReactDOM from "react-dom";

const App = () => {
    const [response, setResponse] = React.useState();
    const handleClick = async () => {
        const text = "Some text here...";
        try {
            const response = await fetch(`/examples/echo?text=${encodeURIComponent(text)}`);
            const responseText = await response.text();
            setResponse(`Response: ${responseText}`);
        } catch (error) {
          	setResponse("Request error!");
        }
    };
    return (
      <div>
        <button onClick={handleClick}>Send request!</button>
        {response && <div>{response}</div>}
      </div>
    );
};

const root = document.querySelector("#root");
ReactDOM.render(<App />, root);
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - button with AJAX request
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join