|
| 1 | +--- |
| 2 | +order: 3 |
| 3 | +title: Use in create-react(-native)-app |
| 4 | +--- |
| 5 | + |
| 6 | +[create-react-app](https://github.com/facebookincubator/create-react-app) (Web project) / [create-react-native-app](https://github.com/react-community/create-react-native-app)(React Native project) is one of best React application development tool, we are going to use `antd-mobile` within it; |
| 7 | + |
| 8 | +## Install and Initialization |
| 9 | + |
| 10 | +We need to install the appropriate tools first, you may need install [yarn](https://github.com/yarnpkg/yarn/) too. |
| 11 | + |
| 12 | +```bash |
| 13 | +$ npm install -g yarn |
| 14 | +$ npm install -g create-react-app (web project) |
| 15 | +$ npm install -g create-react-native-app (react-native project) |
| 16 | +``` |
| 17 | + |
| 18 | +Then we create a new project named antm-demo. |
| 19 | + |
| 20 | +```bash |
| 21 | +$ create-react-app antm-demo (web project) |
| 22 | +$ create-react-native-app antm-demo (react-native project) |
| 23 | +``` |
| 24 | + |
| 25 | +The tool will create and initialize environment and dependencies automaticly, please try config your proxy setting or use other npm registry if any network errors happen during it. |
| 26 | + |
| 27 | +Then we go inside antm-demo and start it. |
| 28 | + |
| 29 | +```bash |
| 30 | +$ cd antm-demo |
| 31 | +$ yarn start |
| 32 | +``` |
| 33 | + |
| 34 | +- `Web project`: Open browser at http://localhost:3000/, it renders a header saying "Welcome to React" on the page. |
| 35 | +- `React Native project`: Run `npm run ios` in terminal, it should be ok if you can see the page content in simulator. |
| 36 | + |
| 37 | +## Import antd-mobile |
| 38 | + |
| 39 | +- ### Web project |
| 40 | + |
| 41 | +- ### React Native project |
| 42 | + |
| 43 | + 1. Now we install antd-mobile and [babel-plugin-import](https://github.com/ant-design/babel-plugin-import)(A babel plugin for importing components on demand [principle](https://github.com/ant-design/ant-design/blob/master/docs/react/getting-started#Import-on-Demand)) from yarn or npm. |
| 44 | + |
| 45 | + ```bash |
| 46 | + $ yarn add antd-mobile |
| 47 | + $ yarn add antd-mobile --dev |
| 48 | + ``` |
| 49 | + 2. Modify the `.babelrc` config, then restart the service. |
| 50 | + |
| 51 | + ```json |
| 52 | + { |
| 53 | + "presets": ["babel-preset-expo"], |
| 54 | + "plugins": [["import", { "libraryName": "antd-mobile" }]], |
| 55 | + "env": { |
| 56 | + ... |
| 57 | + } |
| 58 | + } |
| 59 | + ``` |
| 60 | + 3. Modify the `App.js` file, import `Button` component from antd-mobile. |
| 61 | +
|
| 62 | + ```js |
| 63 | + ... |
| 64 | + import { Button } from 'antd-mobile'; |
| 65 | +
|
| 66 | + ... |
| 67 | + render() { |
| 68 | + return ( |
| 69 | + ... |
| 70 | + <Button>antd-mobile button</Button> |
| 71 | + ... |
| 72 | + ); |
| 73 | + } |
| 74 | + ``` |
| 75 | +
|
| 76 | +## Customize Theme |
| 77 | +
|
| 78 | +- ### Web project |
| 79 | +
|
| 80 | +- ### React Native project |
| 81 | +
|
| 82 | + 1. Create `theme.js` file in the project root, overwrite the theme variables that you want to change, eg: |
| 83 | +
|
| 84 | + ```js |
| 85 | + module.exports = { |
| 86 | + brand_primary: 'red', |
| 87 | + color_link: 'red', |
| 88 | + border_color_base: 'green', |
| 89 | + }; |
| 90 | + ``` |
| 91 | + 2. Create `scripts/custom-rn-theme.js` file in the project root, copy the contents of [rn-custom-ui/scripts/custom-rn-theme.js](https://github.com/ant-design/antd-mobile-samples/blob/master/rn-custom-ui/scripts/custom-rn-theme.js) to `scripts/custom-rn-theme.js`. |
| 92 | +
|
| 93 | + 3. Modify the `start` script in `package.json` like this: |
| 94 | +
|
| 95 | + ```json |
| 96 | + "scripts": { |
| 97 | + ... |
| 98 | + "start": "node scripts/custom-rn-theme && react-native-scripts start", |
| 99 | + ... |
| 100 | + } |
| 101 | +
|
| 102 | + Then restart the service. |
| 103 | +
|
| 104 | + > Note: if you want to overwrite some styles for a single component, please see [ant-design-mobile/issues/1174](https://github.com/ant-design/ant-design-mobile/issues/1174#issuecomment-295256831) (currently support 1.x verion) |
0 commit comments