Skip to content

Instantly share code, notes, and snippets.

@visarts
Last active March 19, 2019 14:52
import React, { useEffect } from 'react'
import { useStore, useActions } from '../store'
function Stocks () {
// stocks is destructured from the store object for convenience
const { stocks } = useStore()
const actions = useActions()
// calls the API on component mount
useEffect(() => {
actions.stocks.fetchStocks()
}, [])
return (
<div>
<ul>
{stocks.data && stocks.data.map(stock => (
<li key={stock.id}>{stock.title}</li>
))}
</ul>
<button onClick={actions.stocks.fetchStocks}>Refresh stocks</button>
</div>
)
}
export default Stocks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment