Skip to content

Instantly share code, notes, and snippets.

View pingan8787's full-sized avatar
🐤
fly...

王平安 pingan8787

🐤
fly...
View GitHub Profile
@pingan8787
pingan8787 / ArrayComponent.tsx
Created August 28, 2022 14:22
ArrayComponent
const ArrayComponent = () => Array(3).fill(<span>Chris1993</span>); // ❌
function App() {
return (
<div className="App">
<ArrayComponent></ArrayComponent>
</div>
);
}
@pingan8787
pingan8787 / ConditionComponent2.tsx
Created August 28, 2022 14:21
ConditionComponent
const ConditionComponent = ({ useRender = false }) =>
useRender ? <span>Render ConditionComponent</span> : <span>error</span>;// ✅
// or
const ConditionComponent = ({ useRender = false }) =>
useRender ? <span>Render ConditionComponent</span> : null;// ✅
@pingan8787
pingan8787 / ConditionComponent.tsx
Created August 28, 2022 14:21
ConditionComponent
const ConditionComponent = ({ useRender = false }) =>
useRender ? <span>Render ConditionComponent</span> : false;// ❌
function App() {
return (
<div className="App">
<ConditionComponent useRender></ConditionComponent>
{/* 'ConditionComponent' cannot be used as a JSX component.
Its return type 'false | Element' is not a valid JSX element.
Type 'boolean' is not assignable to type 'ReactElement<any, any>'.
*/}
@pingan8787
pingan8787 / PropsComponent.tsx
Created August 28, 2022 14:21
PropsComponent
type IProps = React.PropsWithChildren<{ text: string }>;
const PropsComponent = ({ text }: IProps) => <div>{text}</div>;
function App() {
return (
<div className="App">
<PropsComponent text="Hello Chris1993.">
<span>children</span>
</PropsComponent>
</div>
);
@pingan8787
pingan8787 / GenericComponent.tsx
Created August 28, 2022 14:20
GenericComponent
interface GenericProps<T> {
content: T;
}
const GenericComponent = <T extends unknown>(props: GenericProps<T>) => {
const { content } = props;
const component = <>{content}</>;
return <div>{component}</div>;
};
function App() {
return (
@pingan8787
pingan8787 / Generic Components.tsx
Created August 28, 2022 14:20
Generic Components
interface Props<T> {
items: T[];
renderItem: (item: T) => React.ReactNode;
}
const List = <T extends unknown>(props: Props<T>) => {
const { items, renderItem } = props;
const [state, setState] = React.useState<T[]>([]); // You can use type T in List function scope.
return (
<div>
@pingan8787
pingan8787 / FCComponent.tsx
Created August 28, 2022 14:19
FCComponent
type FCProps = { text: string; children?: any };
const FCComponent: React.FC<FCProps> = ({ text = "" }) => <div>{text}</div>;
function App() {
return (
<div className="App">
<FCComponent text="Hello Chris1993.">
<span>children</span>
</FCComponent>
</div>
);
@pingan8787
pingan8787 / ElementComponent.jsx
Created August 28, 2022 14:18
ElementComponent
type FCProps = { text: string };
const ElementComponent = ({ text }: FCProps): JSX.Element => <div>{text}</div>;
function App() {
return (
<div className="App">
<ElementComponent text="Hello Chris1993."></ElementComponent>
</div>
);
}
@pingan8787
pingan8787 / GenericComponent.jsx
Created August 28, 2022 14:18
GenericComponent
interface GenericProps<T> {
content: T;
}
const GenericComponent = <T extends unknown>(props: GenericProps<T>) => {
const { content } = props;
const component = <>{content}</>;
return <div>{component}</div>;
};
function App() {
return (
@pingan8787
pingan8787 / Generic Components.jsx
Created August 28, 2022 14:18
Generic Components
interface Props<T> {
items: T[];
renderItem: (item: T) => React.ReactNode;
}
const List = <T extends unknown>(props: Props<T>) => {
const { items, renderItem } = props;
const [state, setState] = React.useState<T[]>([]); // You can use type T in List function scope.
return (
<div>