Last active
July 19, 2019 13:23
-
-
Save naveennagan/6ba632e0a9b61cd61f79272c8ca7dc86 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'; | |
function useStockHandler(props) { | |
const [value, setStockValue] = useState(null); | |
function handleStatusChange(latestValue) { | |
setStockValue(latestValue); | |
} | |
useEffect(() => { | |
StockAPI.subscribeToStock(props.stock.id, handleStatusChange); | |
return () => { | |
StockAPI.unsubscribeFromStock(props.stock.id, handleStatusChange); | |
}; | |
}); | |
return value; | |
} |
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
function NewYorkExchange(props) { | |
const stockValue = useStockHandler({id:"NewYork"}); | |
return ( | |
<p> The current stock value is {stockValue}</p> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment