Skip to content

Commit

Permalink
docs(examples/react-native): add example with React Native
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jun 5, 2020
1 parent 5d16319 commit 7a219f9
Show file tree
Hide file tree
Showing 13 changed files with 6,953 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/react-native/.expo-shared/assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"12bb71342c6255bbf50437ec8f4441c083f47cdb74bd89160c15e4f43e52a1cb": true,
"40b842e832070c58deac6aa9e08fa459302ee3f9da492c7e77d93d2fbf4a56fd": true
}
14 changes: 14 additions & 0 deletions examples/react-native/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules/**/*
.expo/*
npm-debug.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision
*.orig.*
web-build/
web-report/

# macOS
.DS_Store
71 changes: 71 additions & 0 deletions examples/react-native/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import io from 'socket.io-client';

const socket = io("192.168.0.28:3000"); // replace with the IP of your server, when testing on real devices

export default class App extends Component {
constructor(props) {
super(props);
this.state = {
connected: socket.connected,
currentTransport: socket.connected ? socket.io.engine.transport.name : '-',
lastMessage: ""
};
}

componentDidMount() {
socket.on('connect', () => this.onConnectionStateUpdate());
socket.on('disconnect', () => this.onConnectionStateUpdate());
socket.on('message', (content) => this.onMessage(content));
}

componentWillUnmount() {
socket.off('connect');
socket.off('disconnect');
socket.off('message');
}

onConnectionStateUpdate() {
this.setState({
connected: socket.connected,
currentTransport: socket.connected ? socket.io.engine.transport.name : '-'
});
if (socket.connected) {
socket.io.engine.on('upgrade', () => this.onUpgrade());
} else {
socket.io.engine.off('upgrade');
}
}

onMessage(content) {
this.setState({
lastMessage: content
});
}

onUpgrade() {
this.setState({
currentTransport: socket.io.engine.transport.name
});
}

render() {
return (
<View style={styles.container}>
<Text>State: { this.state.connected ? 'Connected' : 'Disconnected' }</Text>
<Text>Current transport: { this.state.currentTransport }</Text>
<Text>Last message: { this.state.lastMessage }</Text>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
16 changes: 16 additions & 0 deletions examples/react-native/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

# Example with [React Native](https://reactnative.dev/)

![react native logo](assets/react-native.png)

This example shows a basic example based on [Expo](https://expo.io/).

![demo](assets/react-native-demo.gif)

## How to use

```
$ npm ci
$ npm start # run expo
$ node server.js # run the server
```
28 changes: 28 additions & 0 deletions examples/react-native/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"expo": {
"name": "react-native",
"slug": "react-native",
"platforms": [
"ios",
"android",
"web"
],
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true
}
}
}
Binary file added examples/react-native/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/react-native/assets/react-native.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/react-native/assets/splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions examples/react-native/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
};
};
Loading

0 comments on commit 7a219f9

Please sign in to comment.