|
| 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 | +First 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. |
| 40 | + |
| 41 | + ```bash |
| 42 | + $ yarn add antd-mobile |
| 43 | + $ yarn add babel-plugin-import --dev |
| 44 | + ``` |
| 45 | + |
| 46 | +- ### Web project |
| 47 | + |
| 48 | + 1. Modify `config/webpack.config.dev.js` |
| 49 | + |
| 50 | + ```js |
| 51 | + extensions: ['.web.js', '.js', '.json', '.jsx'], |
| 52 | + ... |
| 53 | + rules: [ |
| 54 | + { |
| 55 | + exclude: [ |
| 56 | + ... |
| 57 | + /\.less$/, |
| 58 | + ... |
| 59 | + ] |
| 60 | + }, |
| 61 | + ... |
| 62 | + // Process JS with Babel. |
| 63 | + { |
| 64 | + test: /\.(js|jsx)$/, |
| 65 | + ... |
| 66 | + options: { |
| 67 | + plugins: [ |
| 68 | + ['import', { libraryName: 'antd-mobile', style: true }], |
| 69 | + ], |
| 70 | + cacheDirectory: true, |
| 71 | + } |
| 72 | + }, |
| 73 | + ... |
| 74 | + // It is generally necessary to use the Icon component, need to configure svg-sprite-loader |
| 75 | + { |
| 76 | + test: /\.(svg)$/i, |
| 77 | + loader: 'svg-sprite-loader', |
| 78 | + include: [ |
| 79 | + require.resolve('antd-mobile').replace(/warn\.js$/, ''), // 1. svg files of antd-mobile |
| 80 | + // path.resolve(__dirname, 'src/my-project-svg-foler'), // folder of svg files in your project |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + test: /\.less$/, |
| 85 | + use: [ |
| 86 | + require.resolve('style-loader'), |
| 87 | + require.resolve('css-loader'), |
| 88 | + { |
| 89 | + loader: require.resolve('postcss-loader'), |
| 90 | + options: { |
| 91 | + ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options |
| 92 | + plugins: () => [ |
| 93 | + autoprefixer({ |
| 94 | + browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'], |
| 95 | + }), |
| 96 | + pxtorem({ rootValue: 100, propWhiteList: [] }) |
| 97 | + ], |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + loader: require.resolve('less-loader'), |
| 102 | + options: { |
| 103 | + modifyVars: { "@primary-color": "#1DA57A" }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + ], |
| 107 | + } |
| 108 | + ] |
| 109 | + ``` |
| 110 | + > Note, we only modified webpack.config.dev.js now, if you wish this config working on production environment, you need to update webpack.config.prod.js as well. |
| 111 | + |
| 112 | + 2. Entry html page Required settings: |
| 113 | + |
| 114 | + - Use HD program settings, see [antd-mobile-0.8-以上版本「高清」方案设置](https://github.com/ant-design/ant-design-mobile/wiki/antd-mobile-0.8-%E4%BB%A5%E4%B8%8A%E7%89%88%E6%9C%AC%E3%80%8C%E9%AB%98%E6%B8%85%E3%80%8D%E6%96%B9%E6%A1%88%E8%AE%BE%E7%BD%AE) for details. |
| 115 | + - Use [FastClick](https://github.com/ftlabs/fastclick), ref [#576](https://github.com/ant-design/ant-design-mobile/issues/576) |
| 116 | + - Use Promise fallback support (some Android phones do not support Promise), as follows: |
| 117 | + |
| 118 | + ```js |
| 119 | + if(!window.Promise) { |
| 120 | + document.writeln('<script src="https://as.alipayobjects.com/g/component/es6-promise/3.2.2/es6-promise.min.js"'+'>'+'<'+'/'+'script>'); |
| 121 | + } |
| 122 | + ``` |
| 123 | + |
| 124 | +- ### React Native project |
| 125 | + |
| 126 | + 1. Modify the `.babelrc` config, then restart the service. |
| 127 | + |
| 128 | + ```json |
| 129 | + { |
| 130 | + "presets": ["babel-preset-expo"], |
| 131 | + "plugins": [["import", { "libraryName": "antd-mobile" }]], |
| 132 | + "env": { |
| 133 | + ... |
| 134 | + } |
| 135 | + } |
| 136 | + ``` |
| 137 | + 2. Modify the `App.js` file, import `Button` component from antd-mobile. |
| 138 | + |
| 139 | + ```js |
| 140 | + ... |
| 141 | + import { Button } from 'antd-mobile'; |
| 142 | +
|
| 143 | + ... |
| 144 | + render() { |
| 145 | + return ( |
| 146 | + ... |
| 147 | + <Button>antd-mobile button</Button> |
| 148 | + ... |
| 149 | + ); |
| 150 | + } |
| 151 | + ``` |
| 152 | + |
| 153 | +## Customize Theme |
| 154 | + |
| 155 | +- ### Web project |
| 156 | + |
| 157 | + Please see: [web-custom-ui](https://github.com/ant-design/antd-mobile-samples/tree/master/web-custom-ui) / [web-custom-ui-pro](https://github.com/ant-design/antd-mobile-samples/tree/master/web-custom-ui-pro) |
| 158 | + |
| 159 | +- ### React Native project |
| 160 | + |
| 161 | + 1. Create `theme.js` file in the project root, overwrite the theme variables that you want to change, eg: |
| 162 | + |
| 163 | + ```js |
| 164 | + module.exports = { |
| 165 | + brand_primary: 'red', |
| 166 | + color_link: 'red', |
| 167 | + border_color_base: 'green', |
| 168 | + }; |
| 169 | + ``` |
| 170 | + 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`. |
| 171 | + |
| 172 | + 3. Modify the `start` script in `package.json` like this: |
| 173 | + |
| 174 | + ```json |
| 175 | + "scripts": { |
| 176 | + ... |
| 177 | + "start": "node scripts/custom-rn-theme && react-native-scripts start", |
| 178 | + ... |
| 179 | + } |
| 180 | +
|
| 181 | + Then restart the service. |
| 182 | +
|
| 183 | + > 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