|
| 1 | +### react-native-root-siblings |
| 2 | +--- |
| 3 | + |
| 4 | +Add sibling elements to your app root |
| 5 | + |
| 6 | + |
| 7 | +### Add it to your project |
| 8 | + |
| 9 | +1. Run `npm install react-native-root-siblings --save` |
| 10 | +2. Import library **before** `AppRegistry.registerComponent` |
| 11 | + |
| 12 | +```javascript |
| 13 | +...do something there |
| 14 | +**import 'react-native-root-siblings';** |
| 15 | +...do something else |
| 16 | + |
| 17 | +*AppRegistry.registerComponent('MyApp', () => MyApp);* |
| 18 | + |
| 19 | +``` |
| 20 | + |
| 21 | +### USAGE |
| 22 | + |
| 23 | +``` |
| 24 | +'use strict'; |
| 25 | +import React, { |
| 26 | + AppRegistry, |
| 27 | + View, |
| 28 | + View, |
| 29 | + Component, |
| 30 | + TouchableHighlight, |
| 31 | + Text |
| 32 | +} from 'react-native'; |
| 33 | +import Dimensions from 'Dimensions'; |
| 34 | +
|
| 35 | +// Import library there,it will wrap everything registered by AppRegistry.registerComponent |
| 36 | +// And add or remove other elements after the root component |
| 37 | +**import RootSiblings from 'react-native-root-siblings';** |
| 38 | +
|
| 39 | +var id = 0; |
| 40 | +var elements = []; |
| 41 | +class SiblingsExample extends Component{ |
| 42 | + addSibling = () { |
| 43 | + let sibling = new RootSiblings(<View |
| 44 | + style={[styles.sibling, {top: id * 20}]} |
| 45 | + > |
| 46 | + <Text>I`m No.{id}</Text> |
| 47 | + </View>); |
| 48 | + id++; |
| 49 | + elements.push(sibling); |
| 50 | + }; |
| 51 | +
|
| 52 | + destroySibling = () { |
| 53 | + let lastSibling = elements.pop(); |
| 54 | + lastSibling.destroy(); |
| 55 | + }; |
| 56 | +
|
| 57 | + updateSibling = () { |
| 58 | +
|
| 59 | + }; |
| 60 | +
|
| 61 | + render() { |
| 62 | + return <View style={styles.container}> |
| 63 | + <TouchableHighlight |
| 64 | + style={styles.button} |
| 65 | + onPress={this.addSibling} |
| 66 | + > |
| 67 | + <Text style={styles.buttonText}>Add element</Text> |
| 68 | + </TouchableHighlight> |
| 69 | + <TouchableHighlight |
| 70 | + style={styles.button} |
| 71 | + onPress={this.destroySibling} |
| 72 | + > |
| 73 | + <Text style={styles.buttonText}>Destroy element</Text> |
| 74 | + </TouchableHighlight> |
| 75 | + <TouchableHighlight |
| 76 | + style={styles.button} |
| 77 | + onPress={this.updateSibling} |
| 78 | + > |
| 79 | + <Text style={styles.buttonText}>Update element</Text> |
| 80 | + </TouchableHighlight> |
| 81 | + </View>; |
| 82 | + } |
| 83 | +} |
| 84 | +
|
| 85 | +AppRegistry.registerComponent('SiblingsExample', () => SiblingsExample); |
| 86 | +
|
| 87 | +var styles = StyleSheet.create({ |
| 88 | + container: { |
| 89 | + flex: 1, |
| 90 | + alignItems: 'center', |
| 91 | + justifyContent: 'center', |
| 92 | + backgroundColor: 'green', |
| 93 | + }, |
| 94 | + button: { |
| 95 | + borderRadius: 4, |
| 96 | + padding: 10, |
| 97 | + marginLeft: 10, |
| 98 | + marginRight: 10, |
| 99 | + backgroundColor: '#ccc', |
| 100 | + borderColor: '#333', |
| 101 | + borderWidth: 1, |
| 102 | + }, |
| 103 | + buttonText: { |
| 104 | + color: '#000' |
| 105 | + }, |
| 106 | + sibling: { |
| 107 | + left: 0, |
| 108 | + height: 20, |
| 109 | + width: Dimensions.get('window').width / 2, |
| 110 | + backgroundColor: 'blue' |
| 111 | + } |
| 112 | +}); |
| 113 | +
|
| 114 | +``` |
| 115 | + |
0 commit comments