📌  相关文章
📜  react native paper install - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:19:43.593000             🧑  作者: Mango

React Native Paper Install

React Native Paper is a UI library for React Native that follows material design guidelines. It provides a set of ready-to-use components that you can use to build beautiful and functional mobile apps.

In this tutorial, we will guide you through the process of installing React Native Paper in your React Native project.

Step 1: Create a New Project

The first step is to create a new React Native project. Open your terminal and run the following command:

npx react-native init MyApp

This command will create a new React Native project named "MyApp" in your current directory.

Step 2: Install Dependencies

Once your project is created, navigate to the project directory by running the following command:

cd MyApp

Next, install the following dependencies:

npm install react-native-paper @react-native-community/clipboard @react-native-async-storage/async-storage @react-navigation/native @react-navigation/stack react-native-gesture-handler react-native-reanimated react-native-safe-area-context react-native-screens

These dependencies are required to use React Native Paper components and for navigation.

Step 3: Link Dependencies

Link the dependencies to your project by running the following command:

npx react-native link

This command will link the dependencies and install any required native modules.

Step 4: Add Fonts

React Native Paper requires some fonts to be installed in your project. Download the fonts from the following link and add them to your project:

https://github.com/KyleAMathews/typefaces/tree/master/packages

To add the fonts, create a new directory named "assets/fonts" in your project directory and copy the fonts to this directory. Next, add the following code to your react-native.config.js file:

module.exports = {
  project: {
    ios: {},
    android: {},
  },
  assets: ['./assets/fonts/'],
};

This code will instruct React Native to include the fonts in your project.

Step 5: Setup Theme

React Native Paper comes with a default theme that follows material design guidelines. You can customize the theme by creating your own theme object.

To create a custom theme object, create a new file named theme.js in the root directory of your project and add the following code:

import { DefaultTheme } from 'react-native-paper';

const theme = {
  ...DefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    primary: '#3498db',
    accent: '#f1c40f',
  },
};

export default theme;

This code will create a custom theme object that sets the primary and accent colors.

To use this custom theme in your project, import it in your App.js file and pass it to the Provider component as a prop, like this:

import React from 'react';
import { Provider as PaperProvider } from 'react-native-paper';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import HomeScreen from './screens/HomeScreen';
import DetailsScreen from './screens/DetailsScreen';
import theme from './theme';

const Stack = createStackNavigator();

const App = () => {
  return (
    <PaperProvider theme={theme}>
      <NavigationContainer>
        <Stack.Navigator>
          <Stack.Screen name="Home" component={HomeScreen} />
          <Stack.Screen name="Details" component={DetailsScreen} />
        </Stack.Navigator>
      </NavigationContainer>
    </PaperProvider>
  );
};

export default App;

This code will provide the custom theme to all components in your project.

Conclusion

In this tutorial, we learned how to install React Native Paper in our React Native project, add dependencies, link dependencies, add fonts, and setup a custom theme. With React Native Paper, we can easily create beautiful and functional mobile apps that follow material design guidelines.