Last active
January 17, 2023 23:27
-
-
Save ChristianOConnor/262716410f0ece6de2689a584fb60b70 to your computer and use it in GitHub Desktop.
App.tsx answer for https://stackoverflow.com/questions/75153066/how-do-i-get-the-value-of-a-usestate-hook-from-an-imported-component-in-a-react
This file contains hidden or 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 logo from "./logo.svg"; | |
import "./App.css"; | |
import React, { useState } from "react"; | |
import ToggleSwitch from "./ToggleSwitch"; | |
function App() { | |
const [resp, setResp] = useState(""); | |
const [isToggled, setIsToggled] = useState(false); | |
async function callApi() { | |
const url = `${process.env.REACT_APP_API_URL}/hello`; | |
const helloStream = await fetch(url); | |
const helloText = await helloStream.text(); | |
setResp(helloText as string); | |
} | |
console.log(isToggled); | |
return ( | |
<div className="App"> | |
<header className="App-header"> | |
<img src={logo} className="App-logo" alt="logo" /> | |
<p> | |
Edit <code>src/App.tsx</code> and save to reload. | |
</p> | |
<a | |
className="App-link" | |
href="https://reactjs.org" | |
target="_blank" | |
rel="noopener noreferrer" | |
> | |
Learn React | |
</a> | |
</header> | |
<ToggleSwitch toggledVal={isToggled} setToggled={setIsToggled} /> | |
<br /> | |
<br /> | |
<div className="card"> | |
<button onClick={() => callApi()}>click this to call API</button> | |
<p>{resp}</p> | |
</div> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment