Skip to content

Instantly share code, notes, and snippets.

@hongkheng
Last active May 15, 2018 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongkheng/efd7b72aae3cf258d41fc80baa009bc8 to your computer and use it in GitHub Desktop.
Save hongkheng/efd7b72aae3cf258d41fc80baa009bc8 to your computer and use it in GitHub Desktop.
Ways to define a React component
/*
This gist explains the several ways to define a React component
*/
// Functional components that has no life cycle methods tied to itself
// Using the function
function SimpleFunction(props) {
// passing in the props that is given by React by default
return (
<div>
{props.someCustomProperty}
</div>
)
}
// Using a variable
// style 1
const AnotherFunctional = (props) => {
return (
<div>{props.anotherCustomProp}</div>
)
}
// style 2
const AnotherShortcut = ({ destructedProp, otherProps }) => {
// props are destructed immediately in the arguments
// a little verbose
return (
<div>
<label>{destructedProp}</label>
{otherProps}
</div>
)
}
// style 3
const ComponentNotProps = (customProps) => {
return <div>{customProps}</div>
}
// Usage
// They can be called and used in such ways
const App = () => (
<div>
<SimpleFunction />
<AnotherFunctional />
<AnotherShortcut />
{ComponentNotProps("Hi")}
</div>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment