What is react-native-codegen?
The react-native-codegen package is a tool used in the React Native ecosystem to generate native code for JavaScript components and modules. It helps in creating type-safe interfaces between JavaScript and native code, ensuring that the data passed between them is correctly typed and validated.
What are react-native-codegen's main functionalities?
Generating Native Modules
This feature allows you to define a native module in a JSON format, which react-native-codegen will then use to generate the corresponding native code. This ensures type safety and consistency between JavaScript and native code.
```json
{
"name": "MyModule",
"type": "module",
"methods": {
"myMethod": {
"type": "function",
"params": [
{ "name": "param1", "type": "string" },
{ "name": "param2", "type": "number" }
],
"returnType": "void"
}
}
}
```
Generating Native Components
This feature allows you to define a native component in a JSON format, which react-native-codegen will then use to generate the corresponding native code. This ensures that the component's props are correctly typed and validated.
```json
{
"name": "MyComponent",
"type": "component",
"props": {
"title": { "type": "string" },
"count": { "type": "number" }
}
}
```
Generating Event Emitters
This feature allows you to define an event emitter in a JSON format, which react-native-codegen will then use to generate the corresponding native code. This ensures that the events and their data are correctly typed and validated.
```json
{
"name": "MyEventEmitter",
"type": "eventEmitter",
"events": {
"onEvent": {
"type": "function",
"params": [
{ "name": "eventData", "type": "object" }
]
}
}
}
```
Other packages similar to react-native-codegen
react-native-create-library
The react-native-create-library package is used to create a new native module library for React Native. It provides a boilerplate for creating native modules but does not offer the same level of type safety and code generation as react-native-codegen.
react-native-builder-bob
The react-native-builder-bob package is a CLI tool for creating and building React Native libraries. It focuses on setting up the project structure and build configuration but does not provide code generation capabilities like react-native-codegen.
react-native-codegen
Installation
yarn add --dev react-native-codegen
Note: We're using yarn
to install deps. Feel free to change commands to use npm
3+ and npx
if you like