how to create a library for react native in javascript

Creating a library for React Native in JavaScript is fairly straightforward.

You can use the following steps to create your own library:

  1. Create a new directory for your library and initialize it with npm.
index.tsx
mkdir my-library
cd my-library
npm init
40 chars
4 lines
  1. Install React Native as a peer dependency.
index.tsx
npm install --save react-native
32 chars
2 lines
  1. Write your library code in JavaScript.
index.tsx
// my-library.js
import React from 'react';
import { View, Text } from 'react-native';

export const MyComponent = () => {
  return (
    <View>
      <Text>My Component</Text>
    </View>
  );
};
197 chars
12 lines
  1. Export your components from your library.
index.tsx
// index.js
export { MyComponent } from './my-library';
56 chars
3 lines
  1. Publish your package to npm.
index.tsx
npm login
npm publish
22 chars
3 lines

That's it! Now your React Native library can be installed via npm and used in any React Native application.

gistlibby LogSnag