Skip to content

Commit 4d5a46f

Browse files
committed
yarn gen-readme
1 parent c95e54f commit 4d5a46f

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ function Foo() {
577577

578578
If you are sure that `divRef.current` will never be null, it is also possible to use the non-null assertion operator `!`:
579579

580-
```
580+
```tsx
581581
const divRef = useRef<HTMLDivElement>(null!);
582582
// Later... No need to check if it is null
583583
doSomethingWith(divRef.current);
@@ -1163,12 +1163,8 @@ Relevant for components that accept other React components as props.
11631163

11641164
```tsx
11651165
export declare interface AppProps {
1166-
children1: JSX.Element; // bad, doesnt account for arrays
1167-
children2: JSX.Element | JSX.Element[]; // meh, doesn't accept strings
1168-
children3: React.ReactChildren; // despite the name, not at all an appropriate type; it is a utility
1169-
children4: React.ReactChild[]; // better, accepts array children
1170-
children: React.ReactNode; // best, accepts everything (see edge case below)
1171-
functionChildren: (name: string) => React.ReactNode; // recommended function as a child render prop type
1166+
children?: React.ReactNode; // best, accepts everything React can render
1167+
childrenElement: JSX.Element; // A single React element
11721168
style?: React.CSSProperties; // to pass through style props
11731169
onChange?: React.FormEventHandler<HTMLInputElement>; // form events! the generic parameter is the type of event.target
11741170
// more info: https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase/#wrappingmirroring
@@ -1184,7 +1180,7 @@ Before the [React 18 type updates](https://github.com/DefinitelyTyped/Definitely
11841180

11851181
```tsx
11861182
type Props = {
1187-
children: React.ReactNode;
1183+
children?: React.ReactNode;
11881184
};
11891185

11901186
function Comp({ children }: Props) {
@@ -1726,7 +1722,10 @@ interface ProviderStore {
17261722

17271723
const Context = React.createContext({} as ProviderStore); // type assertion on empty object
17281724

1729-
class Provider extends React.Component<{}, ProviderState> {
1725+
class Provider extends React.Component<
1726+
{ children?: React.ReactNode },
1727+
ProviderState
1728+
> {
17301729
public readonly state = {
17311730
themeColor: "red",
17321731
};
@@ -1776,7 +1775,7 @@ class CssThemeProvider extends React.PureComponent<Props> {
17761775
`forwardRef`:
17771776

17781777
```tsx
1779-
type Props = { children: React.ReactNode; type: "submit" | "button" };
1778+
type Props = { children?: React.ReactNode; type: "submit" | "button" };
17801779
export type Ref = HTMLButtonElement;
17811780
export const FancyButton = React.forwardRef<Ref, Props>((props, ref) => (
17821781
<button ref={ref} className="MyClassName" type={props.type}>
@@ -1791,7 +1790,7 @@ export const FancyButton = React.forwardRef<Ref, Props>((props, ref) => (
17911790
This was done [on purpose](https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43265/). You can make it immutable if you have to - assign `React.Ref` if you want to ensure nobody reassigns it:
17921791

17931792
```tsx
1794-
type Props = { children: React.ReactNode; type: "submit" | "button" };
1793+
type Props = { children?: React.ReactNode; type: "submit" | "button" };
17951794
export type Ref = HTMLButtonElement;
17961795
export const FancyButton = React.forwardRef(
17971796
(
@@ -1891,7 +1890,7 @@ Using `ReactDOM.createPortal`:
18911890
const modalRoot = document.getElementById("modal-root") as HTMLElement;
18921891
// assuming in your html file has a div with id 'modal-root';
18931892

1894-
export class Modal extends React.Component {
1893+
export class Modal extends React.Component<{ children?: React.ReactNode }> {
18951894
el: HTMLElement = document.createElement("div");
18961895

18971896
componentDidMount() {
@@ -1921,7 +1920,7 @@ import { createPortal } from "react-dom";
19211920

19221921
const modalRoot = document.querySelector("#modal-root") as HTMLElement;
19231922

1924-
const Modal: React.FC<{}> = ({ children }) => {
1923+
const Modal: React.FC<{ children?: React.ReactNode }> = ({ children }) => {
19251924
const el = useRef(document.createElement("div"));
19261925

19271926
useEffect(() => {
@@ -2008,7 +2007,7 @@ If you don't want to add a new npm package for this, you can also write your own
20082007
import React, { Component, ErrorInfo, ReactNode } from "react";
20092008

20102009
interface Props {
2011-
children: ReactNode;
2010+
children?: ReactNode;
20122011
}
20132012

20142013
interface State {

0 commit comments

Comments
 (0)