{"id":500,"date":"2023-02-25T14:17:52","date_gmt":"2023-02-25T06:17:52","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=500"},"modified":"2023-04-29T20:49:02","modified_gmt":"2023-04-29T12:49:02","slug":"react-native-learning-open-source-sidebar-component-react-native-side-menu","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-learning-open-source-sidebar-component-react-native-side-menu\/","title":{"rendered":"React Native\u5b66\u4e60\u4e4b\u5f00\u6e90\u4fa7\u8fb9\u680f\u7ec4\u4ef6react-native-side-menu"},"content":{"rendered":"<p><code>react-native-side-menu<\/code>\u5f00\u6e90\u7ec4\u4ef6\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/react-native-community\/react-native-side-menu\">https:\/\/github.com\/react-native-community\/react-native-side-menu<\/a><\/p>\n<p>\u5b89\u88c5\uff1a<code>npm i react-native-side-menu --save<\/code><br \/>\n\u4f7f\u7528\uff1a<code>import SideMenu from &#039;react-native-side-menu&#039;;<\/code><\/p>\n<p><!-- more --><\/p>\n<blockquote>\n<p>\u6ce8\u610f\uff1aMenu\u7ec4\u4ef6\u91cc\u9762\u4f7f\u7528 ScrollView \u5fc5\u987b\u8bbe\u7f6e<code>scrollsToTop={false}<\/code><\/p>\n<\/blockquote>\n<p>\u7528RN\u5b9e\u73b0\u4effQQ\u7684\u4fa7\u8fb9\u680f\u6548\u679c<\/p>\n<p>index.android.js<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n *\/\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    Text,\n    Image,\n    TouchableOpacity,\n    View,\n} from &#039;react-native&#039;;\n\nimport SideMenu from &#039;react-native-side-menu&#039;;\n\nimport Menu from &#039;.\/Menu.js&#039;;  \/\/\u5bfc\u5165\u83dc\u5355\u7ec4\u4ef6\n\nconst uri_image_menu = &#039;http:\/\/image18-c.poco.cn\/mypoco\/myphoto\/20160605\/09\/17351665220160605093956066.png&#039;;\n\nexport default class HomeUI extends Component {\n    constructor(props) {\n        super(props);\n        this.state = {\n            isOpen: false,\n            selectedItem: &#039;About&#039;,\n        };\n    }\n\n    toggle() {\n        this.setState({\n            isOpen: !this.state.isOpen,\n        });\n    }\n\n    updateMenuState(isOpen) {\n        this.setState({ isOpen: isOpen });\n    }\n\n    onMenuItemSelected = (item) =&gt; {\n        this.setState({\n            isOpen: false,\n            selectedItem: item,\n        });\n    }\n\n    render() {\n        const menu = &lt;Menu onItemSelected={this.onMenuItemSelected} \/&gt;;\n        return (\n            &lt;SideMenu\n                menu={menu}\n                isOpen={this.state.isOpen}\n                onChange={(isOpen) =&gt; this.updateMenuState(isOpen) }&gt;\n                &lt;View style={styles.container}&gt;\n                    &lt;Text style={styles.welcome}&gt;\n                        Welcome to React Native!\n                    &lt;\/Text&gt;\n                    &lt;Text style={styles.instructions}&gt;\n                        To get started, edit index.android.js\n                    &lt;\/Text&gt;\n                    &lt;Text style={styles.instructions}&gt;\n                        Press Cmd+R to reload, {&#039;\\n&#039;}\n                        Cmd+Control+Z for dev menu\n                    &lt;\/Text&gt;\n                    &lt;Text style={[styles.instructions, { color: &#039;red&#039; }]}&gt;\n                        \u5f53\u524d\u9009\u4e2d\u7684\u83dc\u5355\u662f: {this.state.selectedItem}\n                    &lt;\/Text&gt;\n                &lt;\/View&gt;\n\n                &lt;Button style={styles.button} onPress={() =&gt; this.toggle() }&gt;\n                    &lt;Image\n                        source={{ uri: uri_image_menu, width: 32, height: 32, }}\n                        \/&gt;\n                &lt;\/Button&gt;\n\n            &lt;\/SideMenu&gt;\n        );\n    }\n}\n\nclass Button extends Component {\n    handlePress(e) {\n        if (this.props.onPress) {\n            this.props.onPress(e);\n        }\n    }\n\n    render() {\n        return (\n            &lt;TouchableOpacity\n                onPress={this.handlePress.bind(this) }\n                style={this.props.style}\n                &gt;\n                &lt;Text&gt;{this.props.children}&lt;\/Text&gt;\n            &lt;\/TouchableOpacity&gt;\n        );\n    }\n}\n\nconst styles = StyleSheet.create({\n    button: {\n        position: &#039;absolute&#039;,\n        top: 20,\n        padding: 10,\n    },\n    caption: {\n        fontSize: 20,\n        fontWeight: &#039;bold&#039;,\n        alignItems: &#039;center&#039;,\n    },\n    container: {\n        flex: 1,\n        justifyContent: &#039;center&#039;,\n        alignItems: &#039;center&#039;,\n        backgroundColor: &#039;#F5FCFF&#039;,\n    },\n    welcome: {\n        fontSize: 20,\n        textAlign: &#039;center&#039;,\n        margin: 10,\n    },\n    instructions: {\n        textAlign: &#039;center&#039;,\n        color: &#039;#333333&#039;,\n        marginBottom: 5,\n    },\n});\n\nAppRegistry.registerComponent(&#039;RNAPP&#039;, () =&gt; RNAPP);<\/code><\/pre>\n<p>Menu.js<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n *\/\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    Dimensions,\n    ScrollView,\n    Text,\n    Image,\n    View,\n} from &#039;react-native&#039;;\n\nconst window = Dimensions.get(&#039;window&#039;);\nconst uri = &#039;http:\/\/uc.discuz.net\/images\/noavatar_small.gif&#039;;\n\nconst styles = StyleSheet.create({\n    menu: {\n        flex: 1,\n        width: window.width,\n        height: window.height,\n        backgroundColor: &#039;gray&#039;,\n        padding: 20,\n    },\n    avatarContainer: {\n        marginBottom: 20,\n        marginTop: 20,\n    },\n    avatar: {\n        width: 48,\n        height: 48,\n        borderRadius: 24,\n        flex: 1,\n    },\n    name: {\n        position: &#039;absolute&#039;,\n        left: 70,\n        top: 20,\n    },\n    item: {\n        fontSize: 16,\n        fontWeight: &#039;300&#039;,\n        paddingTop: 10,\n    },\n});\n\nexport default class Menu extends Component {\n    static propTypes = {\n        onItemSelected: React.PropTypes.func.isRequired,\n    };  \/\/ \u6ce8\u610f\u8fd9\u91cc\u6709\u5206\u53f7\n\n    render() {\n        return (\n            &lt;ScrollView scrollsToTop={false} style={styles.menu}&gt;\n                &lt;View style={styles.avatarContainer}&gt;\n                    &lt;Image\n                        style={styles.avatar}\n                        source={{ uri:uri }}\n                        \/&gt;\n                    &lt;Text style={styles.name}&gt;QQ\u6635\u79f0&lt;\/Text&gt;\n                &lt;\/View&gt;\n\n                &lt;Text\n                    onPress={() =&gt; this.props.onItemSelected(&#039;\u5173\u4e8e\u4f5c\u8005&#039;)}\n                    style={styles.item}\n                    &gt;\n                    \u5173\u4e8e\u4f5c\u8005\n                &lt;\/Text&gt;\n\n                &lt;Text\n                    onPress={() =&gt; this.props.onItemSelected(&#039;\u8bbe\u7f6e&#039;)}\n                    style={styles.item}\n                    &gt;\n                    \u8bbe\u7f6e\n                &lt;\/Text&gt;\n            &lt;\/ScrollView&gt;\n        );\n    }\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>react-native-side-menu\u5f00\u6e90\u7ec4\u4ef6\uff1ahttps:\/\/github.com\/react-nat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-500","post","type-post","status-publish","format-standard","hentry","category-react-native"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=500"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/500\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}