Skip to content

Instantly share code, notes, and snippets.

@lstanard
Last active June 17, 2020 20:42
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 lstanard/2fc8c683ad0eae810b5b108f30cd9c72 to your computer and use it in GitHub Desktop.
Save lstanard/2fc8c683ad0eae810b5b108f30cd9c72 to your computer and use it in GitHub Desktop.
Sample of various patterns and standards for FWI function components (based on https://gist.github.com/markerikson/47fff93c92286db72b22bab5b02e2da3)
import React from "react";
import PropTypes from "prop-types";
const MyComponent = ({ prop1, prop2, className }) => {
let someMessage = "This is a message, it may change";
// If a value could change use `let` and if/else statements rather
// than a function that returns a value.
if (prop1) {
someMessage = "The message has changed based on some condition";
}
// Render conditional components into variables
const conditionalComponent = prop2 ? <SomeComponent /> : <AnotherComponent />
// Keep inline logic out of JSX, or use minimally. Lots of logic
// in JSX is an indicator that the code could be broken into a
// seperate component.
return (
<div className={className}>
{someMessage}
{conditionalComponent}
</div>
)
}
MyComponent.propTypes = {
prop1: PropTypes.string.isRequired,
prop2: PropTypes.bool.isRequired,
className: PropTypes.string,
}
export default MyComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment