Default property value in React component using TypeScript

前端 未结 7 1049
温柔的废话
温柔的废话 2020-11-28 01:51

I can\'t figure out how to set default property values for my components using Typescript.

This is the source code:

class PageState
{
}

export class         


        
7条回答
  •  爱一瞬间的悲伤
    2020-11-28 02:14

    Functional Component

    Actually, for functional component the best practice is like below, I create a sample Spinner component:

    import React from 'react';
    import { ActivityIndicator } from 'react-native';
    import { colors } from 'helpers/theme';
    import type { FC } from 'types';
    
    interface SpinnerProps {
      color?: string;
      size?: 'small' | 'large' | 1 | 0;
      animating?: boolean;
      hidesWhenStopped?: boolean;
    }
    
    const Spinner: FC = ({
      color,
      size,
      animating,
      hidesWhenStopped,
    }) => (
      
    );
    
    Spinner.defaultProps = {
      animating: true,
      color: colors.primary,
      hidesWhenStopped: true,
      size: 'small',
    };
    
    export default Spinner;
    

提交回复
热议问题