mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
* Improved react-side-effect Typings Removed all `any` typings. Added tests which test usage. * Made Lint Less Permissive It doesn't need to be so permissive. * Fixed Whitespace * Added Typings for Server Stuff Used a more robust way than suggested in review. Added tests too. * Fixed Server Types `peek` can return either a `TServerState` or `TState` when `withSideEffect` is provided `mapStateOnServer` (based on runtime). `rewind` always returns `TServerState` when `mapStateOnServer` is provided, otherwise it returns `TState`.
60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import * as React from "react";
|
|
import withSideEffect = require("react-side-effect");
|
|
|
|
interface DocumentTitleProps {
|
|
title: string;
|
|
}
|
|
|
|
type State = string | undefined;
|
|
|
|
interface ServerState {
|
|
innerState: State;
|
|
}
|
|
|
|
const DocumentTitle = () => null;
|
|
|
|
function reducePropsToState(propsList: DocumentTitleProps[]): State {
|
|
const innermostProps = propsList[propsList.length - 1];
|
|
if (innermostProps) {
|
|
return innermostProps.title;
|
|
}
|
|
}
|
|
|
|
function handleStateChangeOnClient(title: State) {
|
|
document.title = title || "";
|
|
}
|
|
|
|
const mapStateOnServer = (state: State): ServerState => ({ innerState: state });
|
|
|
|
const DocumentTitleWithServerMapState = withSideEffect(
|
|
reducePropsToState,
|
|
handleStateChangeOnClient,
|
|
mapStateOnServer
|
|
)(DocumentTitle);
|
|
|
|
const testWithServerMapState = () => {
|
|
const testComponent = () => (
|
|
<DocumentTitleWithServerMapState title="Title" />
|
|
);
|
|
const peekedState:
|
|
| ServerState
|
|
| State = DocumentTitleWithServerMapState.peek();
|
|
const rewindedState: ServerState = DocumentTitleWithServerMapState.rewind();
|
|
};
|
|
|
|
const DocumentTitleNotServer = withSideEffect(
|
|
reducePropsToState,
|
|
handleStateChangeOnClient
|
|
)(DocumentTitle);
|
|
|
|
const testWithoutMapState = () => {
|
|
const testComponent = () => <DocumentTitleNotServer title="asdf" />;
|
|
const peekedState: State = DocumentTitleNotServer.peek();
|
|
const rewindedState: State = DocumentTitleNotServer.rewind();
|
|
};
|
|
|
|
const testInvalidProp = () => (
|
|
// $ExpectError
|
|
<DocumentTitleServer notAValidProp="this should fail" />
|
|
);
|