Type representing stateless functional component
const MyComponent: React.SFC<MyComponentProps> = ...
Type representing stateful class component
class MyComponent extends React.Component<MyComponentProps, State> { ...
Type representing union type of (SFC | Component)
const withState = <P extends WrappedComponentProps>(
WrappedComponent: React.ComponentType<P>,
) => { ...
Type representing a concept of React Element - representation of a native DOM component (e.g. <div />
), or a user-defined composite component (e.g. <MyComponent />
)
const elementOnly: React.ReactElement = <div /> || <MyComponent />;
Type representing any possible type of React node (basically ReactElement (including Fragments and Portals) + primitive JS types)
const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined || <div /> || <MyComponent />;
const Component = ({ children: React.ReactNode }) => ...
Type representing style object in JSX (usefull for css-in-js styles)
const styles: React.CSSProperties = { flexDirection: 'row', ...
const element = <div style={styles} ...
Type representing generic event handler
const handleChange: React.ReactEventHandler<HTMLInputElement> = (ev) => { ... }
<input onChange={handleChange} ... />
Type representing more specific event handler
const handleChange = (ev: React.MouseEvent<HTMLDivElement>) => { ... }
<div onMouseMove={handleChange} ... />
::example='../../playground/src/components/sfc-counter.tsx'::
- spread attributes link
::example='../../playground/src/components/sfc-spread-attributes.tsx'::
::example='../../playground/src/components/stateful-counter.tsx'::
::example='../../playground/src/components/stateful-counter-with-default.tsx'::
- easily create typed component variations and reuse common logic
- common use case is a generic list components
::example='../../playground/src/components/generic-list.tsx'::
simple component using children as a render prop
::example='../../playground/src/components/name-provider.tsx'::
Mouse
component found in Render Props React Docs
::example='../../playground/src/components/mouse-provider.tsx'::
Adds state to a stateless counter
::example='../../playground/src/hoc/with-state.tsx':: ::usage='../../playground/src/hoc/with-state.usage.tsx'::
Adds error handling using componentDidCatch to any component
::example='../../playground/src/hoc/with-error-boundary.tsx':: ::usage='../../playground/src/hoc/with-error-boundary.usage.tsx'::
If you try to use connect
or bindActionCreators
explicitly and want to type your component callback props as () => void
this will raise compiler errors. It happens because bindActionCreators
typings will not map the return type of action creators to void
, due to a current TypeScript limitations.
A decent alternative I can recommend is to use () => any
type, it will work just fine in all possible scenarios and should not cause any typing problems whatsoever. All the code examples in the Guide with connect
are also using this pattern.
If there is any progress or fix in regard to the above caveat I'll update the guide and make an announcement on my twitter/medium (There are a few existing proposals already).
There is alternative way to retain type soundness but it requires an explicit wrapping with
dispatch
and will be very tedious for the long run. See example below:
const mapDispatchToProps = (dispatch: Dispatch<ActionType>) => ({
onIncrement: () => dispatch(actions.increment()),
});
::example='../../playground/src/connected/sfc-counter-connected.tsx':: ::usage='../../playground/src/connected/sfc-counter-connected.usage.tsx'::
::example='../../playground/src/connected/sfc-counter-connected-verbose.tsx':: ::usage='../../playground/src/connected/sfc-counter-connected-verbose.usage.tsx'::
::example='../../playground/src/connected/sfc-counter-connected-extended.tsx':: ::usage='../../playground/src/connected/sfc-counter-connected-extended.usage.tsx'::
Hook for state management like Redux in a function component.
::example='../../playground/src/hooks/use-reducer.tsx'::