Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react(-dom): Update experimental typings #49152

Merged
merged 5 commits into from Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 0 additions & 15 deletions types/react-dom/experimental.d.ts
Expand Up @@ -76,21 +76,6 @@ declare module '.' {
*/
function unstable_createRoot(container: Element | Document | DocumentFragment | Comment, options?: RootOptions): Root;

function unstable_discreteUpdates<R>(callback: () => R): R;

function unstable_discreteUpdates<R, A1>(callback: (a1: A1) => R, a1: A1): R;

function unstable_discreteUpdates<R, A1, A2>(callback: (a1: A1, a2: A2) => R, a1: A1, a2: A2): R;

function unstable_discreteUpdates<R, A1, A2, A3>(
callback: (a1: A1, a2: A2, a3: A3) => R,
a1: A1,
a2: A2,
a3: A3,
): R;

function unstable_flushDiscreteUpdates(): void;

function unstable_flushControlled(callback: () => void): void;

// enableSelectiveHydration feature
Expand Down
9 changes: 0 additions & 9 deletions types/react-dom/test/experimental-tests.tsx
Expand Up @@ -25,14 +25,5 @@ function createBlockingRoot() {
}

function updates() {
// $ExpectType 0
ReactDOM.unstable_discreteUpdates((): 0 => 0);
// $ExpectType number
ReactDOM.unstable_discreteUpdates((foo: number) => foo, 1);
// $ExpectError
ReactDOM.unstable_discreteUpdates((foo: number) => foo);

ReactDOM.unstable_flushDiscreteUpdates();

ReactDOM.unstable_flushControlled(() => {});
}
35 changes: 18 additions & 17 deletions types/react/experimental.d.ts
Expand Up @@ -39,6 +39,15 @@ import React = require('.');
export {};

declare module '.' {
export interface SuspenseProps {
/**
* The presence of this prop indicates that the content is computationally expensive to render.
* In other words, the tree is CPU bound and not I/O bound (e.g. due to fetching data).
* @see {@link https://github.com/facebook/react/pull/19936}
*/
unstable_expectedLoadTime?: number;
}

export type SuspenseListRevealOrder = 'forwards' | 'backwards' | 'together';
export type SuspenseListTailMode = 'collapsed' | 'hidden';

Expand Down Expand Up @@ -94,7 +103,7 @@ declare module '.' {
*/
export const unstable_SuspenseList: ExoticComponent<SuspenseListProps>;

export interface SuspenseConfig extends TimeoutConfig {
export interface SuspenseConfig {
busyDelayMs?: number;
busyMinDurationMs?: number;
}
Expand All @@ -105,17 +114,6 @@ declare module '.' {
config: SuspenseConfig | null | undefined,
): void;

export interface TimeoutConfig {
/**
* This timeout (in milliseconds) tells React how long to wait before showing the next state.
*
* React will always try to use a shorter lag when network and device allows it.
*
* **NOTE: We recommend that you share Suspense Config between different modules.**
*/
timeoutMs: number;
}

// must be synchronous
export type TransitionFunction = () => void | undefined;
// strange definition to allow vscode to show documentation on the invocation
Expand All @@ -139,11 +137,10 @@ declare module '.' {
* A good example of this is a text input.
*
* @param value The value that is going to be deferred
* @param config An optional object with `timeoutMs`
*
* @see https://reactjs.org/docs/concurrent-mode-reference.html#usedeferredvalue
*/
export function unstable_useDeferredValue<T>(value: T, config?: TimeoutConfig | null): T;
export function unstable_useDeferredValue<T>(value: T): T;

/**
* Allows components to avoid undesirable loading states by waiting for content to load
Expand All @@ -164,9 +161,6 @@ declare module '.' {
*/
export function unstable_useTransition(config?: SuspenseConfig | null): [TransitionStartFunction, boolean];

/**
* @private
*/
const opaqueIdentifierBranding: unique symbol;
/**
* WARNING: Don't use this as a `string`.
Expand All @@ -185,4 +179,11 @@ declare module '.' {
};

export function unstable_useOpaqueIdentifier(): OpaqueIdentifier;

/**
* Similar to `useTransition` but allows uses where hooks are not available.
*
* @param callback A _synchronous_ function which causes state updates that can be deferred.
*/
export function unstable_startTransition(scope: TransitionFunction): void;
}
28 changes: 26 additions & 2 deletions types/react/test/experimental.tsx
Expand Up @@ -8,13 +8,12 @@ function useExperimentalHooks() {
const [startTransition, done] = React.unstable_useTransition({
busyMinDurationMs: 100,
busyDelayMs: 200,
timeoutMs: 300,
});
// $ExpectType boolean
done;

// $ExpectType boolean
const deferredToggle = React.unstable_useDeferredValue(toggle, { timeoutMs: 500 });
const deferredToggle = React.unstable_useDeferredValue(toggle);

const [func] = React.useState(() => () => 0);

Expand Down Expand Up @@ -69,3 +68,28 @@ function InvalidOpaqueIdentifierUsage() {

return null;
}

function startTransitionTest() {
function transitionToPage(page: string) {}

React.unstable_startTransition(() => {
transitionToPage('/');
});

// $ExpectError
React.unstable_startTransition(async () => {});
}

function suspenseTest() {
function DisplayData() {
return null;
}

function FlameChart() {
return (
<React.Suspense fallback="computing..." unstable_expectedLoadTime={2000}>
<DisplayData />
</React.Suspense>
);
}
}