Skip to content

Instantly share code, notes, and snippets.

@dsdenes
Last active December 23, 2019 14:19
Show Gist options
  • Save dsdenes/364f5b3b73bcd2a1f21c589829ae508c to your computer and use it in GitHub Desktop.
Save dsdenes/364f5b3b73bcd2a1f21c589829ae508c to your computer and use it in GitHub Desktop.
Fetching in React components, the correct way
import { GistList } from './gists.component'
import { useGists } from './gists.control'
function App () {
const [whichList, setWhichList] = useState(0)
return (
<>
<h1>Fetching GitHub Gists</h1>
<button onClick={() => setWhichList(0)}>List 1</button>
<button onClick={() => setWhichList(1)}>List 2</button>
<h2>Showing list {whichList + 1}</h2>
{whichList === 0 && <GistList useGists={useGists} />}
{whichList === 1 && <GistList useGists={useGists} />}
</>
)
}
// Using CRA boilerplate
const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment