Last active
May 15, 2018 06:54
-
-
Save hongkheng/efd7b72aae3cf258d41fc80baa009bc8 to your computer and use it in GitHub Desktop.
Ways to define a React component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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