Skip to content

Instantly share code, notes, and snippets.

@hckhanh
Last active February 9, 2019 14:07
Show Gist options
  • Save hckhanh/aadce447470103da25e329e5254e4615 to your computer and use it in GitHub Desktop.
Save hckhanh/aadce447470103da25e329e5254e4615 to your computer and use it in GitHub Desktop.
Use useState instead of class component
import React, { useState } from "react";
import DataSource from "../DataSource";
const TextBlock = ({ text }) => <>{text}</>;
export function BlogPost({ id }) {
// State is declared in pair: state value and setState function
const [data, setData] = useState(DataSource.getBlogPost(id));
return <TextBlock text={data} />;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment