Skip to content

Instantly share code, notes, and snippets.

@naveennagan
Last active July 19, 2019 13:23
Show Gist options
  • Save naveennagan/6ba632e0a9b61cd61f79272c8ca7dc86 to your computer and use it in GitHub Desktop.
Save naveennagan/6ba632e0a9b61cd61f79272c8ca7dc86 to your computer and use it in GitHub Desktop.
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;
}
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