Skip to main content

View props (old arch)

info

You may notice, that JS specs contain codegen-related methods, classes, types, etc. to make things more future-proof.

That's because:

  • those elements are available since RN older versions (even from v0.65)
  • those elements are falling back to "old architecture" implementation (e.g. codegenNativeComponent)
  • it introduces type safety for exposed native parts on JS side
  • it's much easier to keep single specification on JS side - when old arch will be dropped, there'll be no need to change anything on JS side

So to make it easier, let's use them, to get you more familiar with it 👍

Boolean prop

src/MyAwesomeViewNativeComponent.ts
import type { HostComponent, ViewProps } from 'react-native';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

// ...

export interface MyAwesomeViewProps extends ViewProps {
enabled: boolean
checked?: boolean
}

// codegenNativeComponent uses requireNativeComponent on old arch
export default codegenNativeComponent<MyAwesomeViewProps>('MyAwesomeView') as HostComponent<MyAwesomeViewProps>;

Number prop

src/MyAwesomeViewNativeComponent.ts
import type { HostComponent, ViewProps } from 'react-native';
import type {
Double,
Float,
Int32,
} from 'react-native/Libraries/Types/CodegenTypes';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

// ...

export interface MyAwesomeViewProps extends ViewProps {
precision: Double
optionalPrecision?: Double
price: Float
discount?: Float
count: Int32
optionalCount?: Int32
}

// codegenNativeComponent uses requireNativeComponent on old arch
export default codegenNativeComponent<MyAwesomeViewProps>('MyAwesomeView') as HostComponent<MyAwesomeViewProps>;

String prop

src/MyAwesomeViewNativeComponent.ts
import type { HostComponent, ViewProps } from 'react-native';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

// ...

export interface MyAwesomeViewProps extends ViewProps {
name: string
label?: string
}

// codegenNativeComponent uses requireNativeComponent on old arch
export default codegenNativeComponent<MyAwesomeViewProps>('MyAwesomeView') as HostComponent<MyAwesomeViewProps>;

Array prop

src/MyAwesomeViewNativeComponent.ts
import type { HostComponent, ViewProps } from 'react-native';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

// ...

export interface MyAwesomeViewProps extends ViewProps {
data: ReadonlyArray<number>
optionalData?: ReadonlyArray<Readonly<{ title: string, description: string }>>
}

// codegenNativeComponent uses requireNativeComponent on old arch
export default codegenNativeComponent<MyAwesomeViewProps>('MyAwesomeView') as HostComponent<MyAwesomeViewProps>;

Object prop

src/MyAwesomeViewNativeComponent.ts
import type { HostComponent, ViewProps } from 'react-native';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

// ...

export interface MyAwesomeViewProps extends ViewProps {
config: Readonly<{ flag: boolean, option: string }>
optionalConfig?: Readonly<{ count: number }>
}

// codegenNativeComponent uses requireNativeComponent on old arch
export default codegenNativeComponent<MyAwesomeViewProps>('MyAwesomeView') as HostComponent<MyAwesomeViewProps>;

Color prop

src/MyAwesomeViewNativeComponent.ts
import type { ColorValue, HostComponent, ViewProps } from 'react-native';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

// ...

export interface MyAwesomeViewProps extends ViewProps {
tintColor: ColorValue
optionalColor?: ColorValue
}

// codegenNativeComponent uses requireNativeComponent on old arch
export default codegenNativeComponent<MyAwesomeViewProps>('MyAwesomeView') as HostComponent<MyAwesomeViewProps>;

Event handler prop

src/MyAwesomeViewNativeComponent.ts
import type { HostComponent, ViewProps } from 'react-native';
import type {
BubblingEventHandler,
DirectEventHandler,
} from 'react-native/Libraries/Types/CodegenTypes';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

// ...

export interface MyAwesomeViewProps extends ViewProps {
onAwesomeBubblingEvent?: BubblingEventHandler<Readonly<{ arg: string }>>
onAwesomeDirectEvent?: DirectEventHandler<Readonly<{ arg: string }>>
}

// codegenNativeComponent uses requireNativeComponent on old arch
export default codegenNativeComponent<MyAwesomeViewProps>('MyAwesomeView') as HostComponent<MyAwesomeViewProps>;