Skip to content

Instantly share code, notes, and snippets.

View DoctorDerek's full-sized avatar
☀️
https://linkedin.com/in/derek-austin Read my blog https://DoctorDerek.medium.com

Dr. Derek Austin DoctorDerek

☀️
https://linkedin.com/in/derek-austin Read my blog https://DoctorDerek.medium.com
View GitHub Profile
// Export default const in JavaScript and TypeScript
const Banana = () => (<div>🍌</div>)
export default Banana // Has to be on a separate line
// Default exports in JavaScript and TypeScript
function Banana() {
return <div>🍌</div>
}
const price = 1.33
// In this example, price is a named export:
export { Banana as default, price }
// Named exports in JavaScript and TypeScript
function Banana() {
return <div>🍌</div>
}
const price = 1.33
export { Banana, price }
// These are equivalent to the export syntax above:
@DoctorDerek
DoctorDerek / type-ReactNode.tsx
Created October 21, 2022 17:45
What Is the Correct Type for the Children Prop in React TypeScript? https://medium.com/p/6974e82917b4
type ReactNode =
| ReactElement
| string
| number
| ReactFragment
| ReactPortal
| boolean
| null
| undefined
@DoctorDerek
DoctorDerek / right-type-for-children-with-type-instead-of-interface.tsx
Created October 21, 2022 17:44
What Is the Correct Type for the Children Prop in React TypeScript? https://medium.com/p/6974e82917b4
import { ReactNode } from "react"
type ILayout = {
pageInfo?: { name: string; description: string }
children: ReactNode
}
export default ILayout
@DoctorDerek
DoctorDerek / right-type-for-children.tsx
Created October 21, 2022 17:43
What Is the Correct Type for the Children Prop in React TypeScript? https://medium.com/p/6974e82917b4
import { ReactNode } from "react"
export default interface ILayout {
pageInfo?: { name: string; description: string }
children: ReactNode
}
@DoctorDerek
DoctorDerek / wrong-children-type.tsx
Created October 21, 2022 17:42
What Is the Correct Type for the Children Prop in React TypeScript? https://medium.com/p/6974e82917b4
import React from "react"
interface ILayout {
pageInfo?: { name: string; description: string }
children:
| JSX.Element[]
| JSX.Element
| React.ReactElement
| React.ReactElement[]
| string
@DoctorDerek
DoctorDerek / Partial Object Destructuring of React Props With ... Rest Parameters Syntax.jsx
Created October 12, 2022 16:28
Partial Object Destructuring of React Props With ... Rest Parameters Syntax https://medium.com/p/4e8629a02035
const CardComponent = ({ name, ...props }) => <div>name</div>
// That format is equivalent to including the following code:
const { name } = props
// As in the above example, there could be other stuff in props.
@DoctorDerek
DoctorDerek / Partial Object Destructuring of React Props With ... Rest Parameters Syntax.jsx
Created October 12, 2022 16:28
Partial Object Destructuring of React Props With ... Rest Parameters Syntax https://medium.com/p/4e8629a02035
function ChildComponent(props) {
// There could be other props that we didn't destructure here.
const { firstName, lastName } = props
return (
<span>
{firstName} {lastName}
</span>
)
}
@DoctorDerek
DoctorDerek / .eslintrc.js
Created October 12, 2022 16:02
The right way to configure ESLint with Prettier https://medium.com/p/52f278b2f47f
{
"extends": [
"prettier"
]
}