Add types for react-lifecycle-component (#28635)

Please fill in this template.

- [x] Use a meaningful title for the pull request. Include the name of the package modified.
- [x] Test the change in your own code. (Compile and run.)
- [x] Add or edit tests to reflect the change. (Run with `npm test`.)
- [x] Follow the advice from the [readme](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#make-a-pull-request).
- [x] Avoid [common mistakes](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/README.md#common-mistakes).
- [x] Run `npm run lint package-name` (or `tsc` if no `tslint.json` is present).

Select one of these and delete the others:

If adding a new definition:
- [x] The package does not already provide its own types, or cannot have its `.d.ts` files generated via `--declaration`
- [x] If this is for an NPM package, match the name. If not, do not conflict with the name of an NPM package.
- [x] Create it with `dts-gen --dt`, not by basing it on an existing project.
- [x] `tslint.json` should be present, and `tsconfig.json` should have `noImplicitAny`, `noImplicitThis`, `strictNullChecks`, and `strictFunctionTypes` set to `true`.
This commit is contained in:
Alexander Fisher 2018-09-10 20:04:18 -04:00 committed by Ryan Cavanaugh
parent 7f376606df
commit 8a155514f4
5 changed files with 154 additions and 0 deletions

View File

@ -0,0 +1,24 @@
// Type definitions for react-lifecycle-component 2.0
// Project: https://github.com/JamieDixon/react-lifecycle-component
// Definitions by: Alexander Fisher <https://github.com/pixelshaded>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
import { ComponentLifecycle, Component, ComponentClass } from 'react';
import { Connect } from 'react-redux';
export interface LifecycleStateProps<P = {}, S = {}> {
component: ComponentClass<P, S>;
}
export interface LifecycleDispatchProps<P = {}, S = {}> extends ComponentLifecycle<P, S> {}
export interface Props<P = {}, S = {}> extends LifecycleStateProps<P, S>, LifecycleDispatchProps<P, S> {}
export class LifecycleComponent extends Component<Props<any, any>, any> {}
export function applyLifecycle<P = {}, S = {}>(
component: ComponentClass<P, S>,
): ComponentClass<P & LifecycleDispatchProps<P, S>>;
export const connectWithLifecycle: Connect;

View File

@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"redux": "^4.0.0"
}
}

View File

@ -0,0 +1,98 @@
import * as React from 'react';
import { Dispatch } from 'redux';
import { connect } from 'react-redux';
import {
connectWithLifecycle,
LifecycleDispatchProps,
applyLifecycle,
LifecycleStateProps,
LifecycleComponent,
} from 'react-lifecycle-component';
interface State {
stateFoo: number;
}
interface Props extends StateProps, DispatchProps {
}
interface StateProps {
propsFoo: number;
}
interface DispatchProps {
bar(): void;
}
class ComponentFoo extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
stateFoo: 0,
};
}
render() {
return (
<div>
<div>Props Foo: {this.props.propsFoo}</div>
<div>State Foo: {this.state.stateFoo}</div>
<button onClick={this.props.bar}>Bar</button>
</div>
);
}
}
type MapStateProps = StateProps & LifecycleStateProps<Props>;
function mapStateToProps(): MapStateProps {
return {
component: ComponentFoo,
propsFoo: 8675309,
};
}
type MapDispatchProps = DispatchProps & LifecycleDispatchProps<Props, State>;
function mapDispatchToProps(dispatch: Dispatch): MapDispatchProps {
return {
bar() {
dispatch({ type: 'Bar'});
},
componentWillMount() {
dispatch({ type: 'ComponentWillMount'});
},
componentDidMount() {
dispatch({ type: 'ComponentDidMount'});
},
componentWillUpdate(nextProps, nextState, nextContext) {
const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo;
const hasNextContext: boolean = !!nextContext;
dispatch({ type: 'ComponentWillUpdate'});
},
componentDidUpdate(nextProps, nextState, nextContext) {
const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo;
const hasNextContext: boolean = !!nextContext;
dispatch({ type: 'ComponentDidUpdate'});
},
componentWillReceiveProps(nextProps, nextContext) {
const fooIsGreaterThanZero: boolean = nextProps.propsFoo > 0;
const hasNextContext: boolean = !!nextContext;
dispatch({ type: 'ComponentWillReceiveProps'});
},
componentWillUnmount() {
dispatch({ type: 'ComponentWillUnmount'});
},
shouldComponentUpdate(nextProps, nextState, nextContext) {
const fooIsEqual: boolean = nextProps.propsFoo === nextState.stateFoo;
const hasNextContext: boolean = !!nextContext;
return !fooIsEqual;
},
};
}
const connectWithLifecylceContainer =
connectWithLifecycle<StateProps, MapDispatchProps>(mapStateToProps, mapDispatchToProps)(ComponentFoo);
const applyLifecycleContainer =
connect<StateProps, MapDispatchProps>(mapStateToProps, mapDispatchToProps)(applyLifecycle(ComponentFoo));
const lifecycleContainer =
connect<MapStateProps, MapDispatchProps>(mapStateToProps, mapDispatchToProps)(LifecycleComponent);

View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"jsx": "react",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"react-lifecycle-component-tests.tsx"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }