Skip to content

Commit 9ad478d

Browse files
author
Lucas Bento
authored
Refactor to export HOC, handle socket in lifecycle methods & add option to show requests in console
2 parents 2b82f11 + 9023495 commit 9ad478d

File tree

8 files changed

+435
-45
lines changed

8 files changed

+435
-45
lines changed

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,33 @@ code --install-extension lucasbento.react-native-actions
2626
2727
### React Native
2828

29-
> Only required for iOS.
30-
3129
1. Install the dependency
3230
```bash
3331
yarn add react-native-actions --dev
3432
```
3533

36-
2. Link on iOS
34+
2. Link (Only required for iOS)
3735
```bash
3836
react-native link react-native-actions
3937
```
4038

4139
> You can check out the [manual installation here](https://github.com/lucasbento/react-native-actions/blob/master/packages/react-native-actions/README.md#manual-installation).
4240
43-
3. Simply put this on your root file (e.g. `index.js`):
44-
```js
45-
if (__DEV__) {
46-
require('react-native-actions');
41+
3. Simply wrap your root component with `withActions` [HOC](https://reactjs.org/docs/higher-order-components.html):
42+
```jsx
43+
// Your main component
44+
45+
import withActions from 'react-native-actions';
46+
47+
class Application extends Component {
48+
// ...
4749
}
50+
51+
export default withActions(Application);
4852
```
4953

5054
### Usage
5155

52-
- <kbd>⌘</kbd> + <kbd>Shift</kbd> + <kbd>R</kbd>: to reload the code on device.
53-
- <kbd>⌘</kbd> + <kbd>Shift</kbd> + <kbd>D</kbd>: to open developer menu.
56+
- <kbd>⌘</kbd> + <kbd>Shift</kbd> + <kbd>R</kbd>: to reload the code on device;
57+
- <kbd>⌘</kbd> + <kbd>Shift</kbd> + <kbd>D</kbd>: to open developer menu;
58+
- <kbd>⌘</kbd> + <kbd>Shift</kbd> + <kbd>T</kbd>: to show network requests in console (requires remote debugging).

packages/react-native-actions/README.md

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,32 @@
77
Run React Native actions from within VSCode.
88
</p>
99

10+
<p align="center">
11+
<a href="https://github.com/lucasbento/react-native-actions/issues"><img src="https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat"></a>
12+
<a href="https://saythanks.io/to/lucasbento"><img src="https://img.shields.io/badge/say-thanks-ff69b4.svg"></a>
13+
</p>
14+
15+
## Features
16+
17+
- Reload the code in device;
18+
- Open developer menu;
19+
- Show network requests in console (requires remote debugging).
20+
21+
This package is inteded to be used with [VSCode React Native Actions](https://marketplace.visualstudio.com/items?itemName=lucasbento.react-native-actions) extension.
22+
1023
## Getting started
1124

1225
```bash
1326
yarn add react-native-actions --dev
1427
```
1528

16-
### Automatic installation
29+
### Automatic installation (only required for iOS)
1730

1831
```bash
1932
react-native link react-native-actions
2033
```
2134

22-
### Manual installation
35+
### Manual installation (only required for iOS)
2336

2437
#### iOS
2538

@@ -32,10 +45,16 @@ react-native link react-native-actions
3245

3346
Android doesn't require any dependency installed through react-native, only [Android Debug Bridge (adb)](https://developer.android.com/studio/command-line/adb.html) on the computer.
3447

35-
## Usage (only required for iOS)
36-
```js
37-
if (__DEV__) {
38-
require('react-native-actions');
48+
## Usage
49+
```jsx
50+
// Your main component
51+
52+
import withActions from 'react-native-actions';
53+
54+
class Application extends Component {
55+
// ...
3956
}
57+
58+
export default withActions(Application);
4059
```
4160

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,94 @@
1+
import React, { Component } from 'react';
12
import { Platform, NativeModules } from 'react-native';
3+
import hoistNonReactStatics from 'hoist-non-react-statics';
4+
import Toast from 'react-native-easy-toast';
25
import io from 'socket.io-client';
36

47
import config from './common/config';
58

6-
const run = () => {
7-
// Android is handled through adb
8-
if (Platform.OS === 'android') {
9-
return;
10-
}
9+
const DEFAULT_XMLHTTPREQUEST = GLOBAL.XMLHttpRequest;
10+
const isDev = __DEV__;
11+
12+
const withActions = (WrappedComponent) => {
13+
class RNActions extends Component {
14+
state = {
15+
isShowRequestEnabled: false,
16+
};
17+
18+
componentWillMount() {
19+
this.handleMount();
20+
}
21+
22+
componentWillUnmount() {
23+
this.handleUnmount();
24+
}
25+
26+
toggleShowRequest = () =>
27+
this.setState(({ isShowRequestEnabled }) => ({
28+
isShowRequestEnabled: !isShowRequestEnabled,
29+
}), () => {
30+
const toastMessage = this.state.isShowRequestEnabled ?
31+
'Enabled showing requests in console' :
32+
'Disabled showing requests in console';
1133

12-
const { RNActions, DevMenu, DevSettings } = NativeModules;
34+
this.toast.show(toastMessage);
1335

14-
const COMMANDS = {
15-
reload: DevSettings.reload,
16-
openDevMenu: DevMenu.show,
17-
};
36+
GLOBAL.XMLHttpRequest = this.state.isShowRequestEnabled ?
37+
GLOBAL.originalXMLHttpRequest :
38+
DEFAULT_XMLHTTPREQUEST;
39+
});
40+
41+
handleMount = () => {
42+
if (!isDev) {
43+
return;
44+
}
45+
46+
const { RNActions: NativeRNActions } = NativeModules;
47+
48+
NativeRNActions.getHostUrl()
49+
.then(this.handleSetupSocket);
50+
};
51+
52+
handleSetupSocket = (url) => {
53+
const { DevMenu, DevSettings } = NativeModules;
54+
55+
this.socket = io(`${url}:${config.port}`);
56+
57+
const COMMANDS = {
58+
...Platform.select({
59+
ios: {
60+
reload: DevSettings.reload,
61+
openDevMenu: DevMenu.show,
62+
},
63+
android: {},
64+
}),
65+
toggleShowRequest: this.toggleShowRequest,
66+
};
67+
68+
this.socket.on('action', ({ type }) => COMMANDS[type]());
69+
};
70+
71+
handleUnmount = () => this.socket.disconnect();
72+
73+
render() {
74+
if (isDev) {
75+
return [
76+
<Toast
77+
key="toast"
78+
ref={ref => this.toast = ref}
79+
/>,
80+
<WrappedComponent
81+
key="main"
82+
{...this.props}
83+
/>,
84+
];
85+
}
1886

19-
RNActions.getHostUrl()
20-
.then((url) => {
21-
const socket = io(`${url}:${config.port}`);
87+
return <WrappedComponent {...this.props} />;
88+
}
89+
}
2290

23-
socket.on('action', ({ type }) => COMMANDS[type]());
24-
});
25-
}
91+
return hoistNonReactStatics(RNActions, WrappedComponent);
92+
};
2693

27-
run();
94+
export default withActions;

packages/react-native-actions/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"url": "https://github.com/lucasbento/react-native-actions/issues"
1212
},
1313
"dependencies": {
14+
"hoist-non-react-statics": "^2.3.1",
15+
"react-native-easy-toast": "^1.0.9",
1416
"socket.io-client": "^2.0.3"
1517
},
1618
"keywords": [

0 commit comments

Comments
 (0)