Last active
June 15, 2019 13:10
-
-
Save pflevy/866954af44a66652194b6e69dbf56c86 to your computer and use it in GitHub Desktop.
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, { useState, useEffect } from "react"; | |
import "./style.css"; | |
import Canvas from "./canvas"; | |
import firebase from "./firebase"; | |
const App = () => { | |
// Define the state of the component | |
const [distance, setDistance] = useState(0); | |
// Listen to changes on the firebase database, specifically the "distance" entry | |
useEffect(() => { | |
const getValue = firebase.database().ref("distance"); | |
getValue.on("value", snapshot => { | |
let value = snapshot.val(); | |
// Whenever the value changes on the server, it is also reset on the running app through the variable | |
setDistance(value.toFixed(2)); | |
}); | |
}, []); | |
return ( | |
<div className="litreDisplay"> | |
<div className="canvas"> | |
{/* The variable is passed down to the Canvas component, which will re-render every time its altered*/} | |
<Canvas distance={distance} /> | |
</div> | |
<div className="displayValue"> | |
<span>{distance}cm</span> | |
</div> | |
</div> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment