Last active
January 28, 2022 16:29
-
-
Save jhonsore/e439bcf1eafe5220193a337a49bca699 to your computer and use it in GitHub Desktop.
React tips
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
// the interface needs to explicitly declare which strings are safe to pass | |
interface HeadingProps extends React.HTMLAttributes<HTMLHeadingElement> { | |
headingLevel: "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "p" | |
} | |
const TsHeading = ({ | |
headingLevel = "p", | |
children, | |
className, | |
}: HeadingProps) => { | |
const Heading = ({ ...props }: React.HTMLAttributes<HTMLHeadingElement>) => | |
React.createElement(headingLevel, props, children) | |
return <Heading className={className}>{children}</Heading> | |
} | |
const Banner: React.FC<{ headingText: string; description: string }> = ({ | |
headingText, | |
description, | |
}) => ( | |
<div> | |
<TsHeading headingLevel="h2">{headingText}</TsHeading> | |
<p>{description}</p> | |
</div> | |
) |
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
import { ChangeEvent } from 'react'; | |
import React = require('react'); | |
interface Props extends React.InputHTMLAttributes<HTMLInputElement> { | |
name?: string; | |
label?: string; | |
} | |
export const Input: React.FC<Props> = ({ ...args }) => { | |
const handleInput = (event: ChangeEvent<HTMLInputElement>) => { | |
event.preventDefault(); | |
console.log(event.target.value); | |
}; | |
return ( | |
<div> | |
<input type="text" {...args} onChange={handleInput} /> | |
</div> | |
); | |
}; |
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
import { ChangeEvent } from 'react'; | |
import React = require('react'); | |
interface Props extends React.SelectHTMLAttributes<HTMLSelectElement> {} | |
export const Select: React.FC<Props> = ({ ...args }) => { | |
const selectChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | |
event.preventDefault(); | |
console.log(event.target.value); | |
}; | |
return ( | |
<div> | |
<select onChange={selectChange} {...args}> | |
<option selected disabled> | |
Choose one | |
</option> | |
<option value="blue">Blue</option> | |
<option value="red">Red</option> | |
<option value="green">Green</option> | |
<option value="yellow">Yellow</option> | |
<option value="kindacode.com">Kindacode.com</option> | |
</select> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment