import { createElement, render, Component, ComponentProps, FunctionalComponent, AnyComponent, h } from '../../'; interface DummyProps { initialInput: string; } interface DummyState { input: string; } class DummyComponent extends Component { constructor(props: DummyProps) { super(props); this.state = { input: `x${this.props}x` }; } private setRef = (el: AnyComponent) => { console.log(el); }; render({ initialInput }: DummyProps, { input }: DummyState) { return (
{/* Can specify all Preact attributes on a typed FunctionalComponent */}
); } } interface DummerComponentProps extends DummyProps, DummyState {} function DummerComponent({ input, initialInput }: DummerComponentProps) { return (
Input: {input}, initial: {initialInput}
); } render(createElement('div', { title: 'test', key: '1' }), document); render( createElement(DummyComponent, { initialInput: 'The input', key: '1' }), document ); render( createElement(DummerComponent, { initialInput: 'The input', input: 'New input', key: '1' }), document ); render(h('div', { title: 'test', key: '1' }), document); render(h(DummyComponent, { initialInput: 'The input', key: '1' }), document); render( h(DummerComponent, { initialInput: 'The input', input: 'New input', key: '1' }), document ); // Accessing children const ComponentWithChildren: FunctionalComponent = ({ input, initialInput, children }) => { return (
{initialInput} {input} {children}
); }; const UseOfComponentWithChildren = () => { return ( child 1 child 2 ); }; // using ref and or jsx class ComponentUsingRef extends Component { private array: string[]; private refs: (Element | null)[] = []; constructor() { super(); this.array = ['1', '2']; } render() { this.refs = []; return (
{this.array.map(el => ( {el} ))} {/* Can specify Preact attributes on a component */}
); } private setRef = (el: Element | null) => { this.refs.push(el); }; } // using lifecycles class ComponentWithLifecycle extends Component { render() { return
Hi
; } componentWillMount() { console.log('componentWillMount'); } componentDidMount() { console.log('componentDidMount'); } componentWillUnmount() { console.log('componentWillUnmount'); } componentWillReceiveProps(nextProps: DummyProps, nextCtx: any) { const { initialInput } = nextProps; console.log('componentWillReceiveProps', initialInput, nextCtx); } shouldComponentUpdate( nextProps: DummyProps, nextState: DummyState, nextContext: any ) { return false; } componentWillUpdate( nextProps: DummyProps, nextState: DummyState, nextContext: any ) { console.log('componentWillUpdate', nextProps, nextState, nextContext); } componentDidUpdate( previousProps: DummyProps, previousState: DummyState, previousContext: any ) { console.log( 'componentDidUpdate', previousProps, previousState, previousContext ); } } // Default props: JSX.LibraryManagedAttributes class DefaultProps extends Component<{ text: string; bool: boolean }> { static defaultProps = { text: 'hello' }; render() { return
{this.props.text}
; } } const d1 = ; const d2 = ; class DefaultPropsWithUnion extends Component< { default: boolean } & ( | { type: 'string'; str: string; } | { type: 'number'; num: number; } ) > { static defaultProps = { default: true }; render() { return
; } } const d3 = ; const d4 = ; const d5 = ; const d6 = ; class DefaultUnion extends Component< | { type: 'number'; num: number; } | { type: 'string'; str: string; } > { static defaultProps = { type: 'number', num: 1 }; render() { return
; } } const d7 = ; const d8 = ; const d9 = ; const d10 = ; class ComponentWithDefaultProps extends Component<{ value: string }> { static defaultProps = { value: '' }; render() { return
{this.props.value}
; } } const withDefaultProps = ; interface PartialState { foo: string; bar: number; } class ComponentWithPartialSetState extends Component<{}, PartialState> { render({}, { foo, bar }: PartialState) { return ( ); } handleClick = (value: keyof PartialState) => { this.setState({ [value]: 'updated' }); }; } const withPartialSetState = ; let functionalProps: ComponentProps = { initialInput: '', input: '' }; let classProps: ComponentProps = { initialInput: '' }; let elementProps: ComponentProps<'button'> = { type: 'button' }; // Typing of style property const acceptsNumberAsLength =
; const acceptsStringAsLength =
;