3

I'm quite new to React-Native. I'm trying to add sideBar/hamburger menu to my application with implementing 'react-native drawer' component. Firstly, I'm trying to add the example code from GitHub to my new test project just to understand how it works. I face with the error in the screen.

It would make me really happy, If I get some help. Or can you advice me easier way to implement sideBar/hamburger menu to my project.

import Drawer from 'react-native-drawer';
import React, {Component} from 'react';
import SideBarContent from '../common/SideBarContent';
import {Text,View} from 'react-native';

class SideBar extends Component{

    closeControlPanel = () => {
        this._drawer.close()
    };
    openControlPanel = () => {
        this._drawer.open()
    };

    render()
    {
        const drawerStyles = {
            drawer: { shadowColor: '#000000', shadowOpacity: 0.8, shadowRadius: 3},
            main: {paddingLeft: 3},
        }


        return (
            <Drawer
                type="static"
                content={<SideBarContent/>}
                openDrawerOffset={100}
                styles={drawerStyles}
                tweenHandler={Drawer.tweenPresets.parallax}
            >
                <View><Text>Drawer</Text></View>
            </Drawer>
        );
    }
}

and here is my SideBarContent Component.

import React, {Component} from 'react';
import {Text,View} from 'react-native';


class SideBarContent extends Component{
    render()
    {
        return(
            <View>
                <Text>Order History</Text>
                <Text>Account</Text>
                <Text>Basket</Text>
                <Text>About us</Text>
            </View>
        );
    }
}

enter image description here

Ali Zeynalov
  • 2,867
  • 8
  • 30
  • 54
  • The `` and `` elements are custom elements which are not part of the React Native library. You could replace them with your own content eg. `
    ControlPanel
    ` to see how it looks
    – Jackson Mar 12 '17 at 12:01

2 Answers2

6

As I am not on system so haven't check the working of code, but this should work.

import Drawer from 'react-native-drawer';
import React, {Component} from 'react';
import SideBarContent from '../common/SideBarContent';
import {Text,View} from 'react-native';

export default class SideBar extends Component{

    constructor(){
        super();
        this.closeControlPanel = this.closeControlPanel.bind(this);
        this.openControlPanel = this.openControlPanel.bind(this);
    }

    closeControlPanel = () => {
        this._drawer.close()
    };
    openControlPanel = () => {
        this._drawer.open()
    };

    render()
    {
        const drawerStyles = {
            drawer: { shadowColor: '#000000', shadowOpacity: 0.8, shadowRadius: 3},
            main: {paddingLeft: 3},
        }

        return (
            <Drawer
                type="static"
                content={<SideBarContent />}
                ref = {(ref) => this._drawer = ref}
                openDrawerOffset={100}
                styles={drawerStyles}
                tweenHandler={Drawer.tweenPresets.parallax}
            >
                <View>
                <Text onPress={this.openControlPanel}>
                    Drawer
                </Text>
                </View>
            </Drawer>
        );
    }
}

An another file SideBarContent

 import React, {Component} from 'react';
    import {Text,View} from 'react-native';


    export default class SideBarContent extends Component{
        constructor() {
            super();
        }
        render()
        {
            return(
                <View>
                    <Text>Order History</Text>
                    <Text>Account</Text>
                    <Text>Basket</Text>
                    <Text>About us</Text>
                </View>
            );
        }
    }

Please let me know if any issues..

Neel Gala
  • 2,350
  • 1
  • 19
  • 20
3

the <MainView /> is essentially the screen that you will be showing before the hamburger menu.

The <ControlPanel /> is the side view that you will show after the user clicks on the Hamburger menu. In other words, it is the actual side menu.

The <Drawer /> controls the opening/closing of these views, animations, other functionalities of a drawer/side view/ side menu (whatever you wish to call it).

You still need to create these views. I'll help you with that showing an example of an app of mine.

My <MainView/> is this screen below: MainView

My <ControlPanel /> is this one: ControPanel

I also use the same library you are trying to use.

Hope this helps.

Gui Herzog
  • 5,327
  • 2
  • 18
  • 22
  • thank you very much. It almost made everything clear for me. I created new component for 'content' instead of ControlPanel and changed the MainView with simple 'View' component, just to see the hamburger menu and it's content. I edited my code with current one, so you can see. But, now, I get 'check render method of Default Renderer' error, which I screened and added to the question, as wel, so you can see. Do you have any idea what I am doing wrong? I could not get after some search on google. – Ali Zeynalov Mar 12 '17 at 14:14
  • 1
    Let me check for you. – Gui Herzog Mar 12 '17 at 14:26
  • I also added 'SideBarContent' component's code that I use for 'content'. I do not know, maybe I do something wrong there. – Ali Zeynalov Mar 12 '17 at 14:31
  • My guess is that you imported the View from react native inside the brackets and when you are using it inside the drawer, the code is breaking. I have to look here, but anyway try to create a component with a simple view and import it or try to import the view outside the brackets on your imports. I don't know if it's gonna work, but if it doesn't I can look later for you. – Gui Herzog Mar 12 '17 at 14:34
  • thank you. I tried both of your advices. Wheras, nothing changed. I would appreciate your help, when you're available. – Ali Zeynalov Mar 12 '17 at 14:46
  • Hmm, sad. I'll take a look here. Give me 10 min. – Gui Herzog Mar 12 '17 at 14:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137882/discussion-between-guilherme-herzog-and-yasharoglu-zeynal). – Gui Herzog Mar 12 '17 at 15:03
  • 1
    Oh, yeah you forgot to export the SideBarContent class. Every time you create an external component you should add: export default at the beginning. E.g. export default class SideBarContent ... – Gui Herzog Mar 12 '17 at 15:17
  • They're just an example. You can create any view, but if you are looking specifically for the design, I can help you with that. – Gui Herzog Jul 11 '17 at 13:30
  • @Guilherme Herzog I'm looking for to navigate some component inside the control panel. is that possible ? – Balasubramanian Sep 02 '17 at 06:31
  • Well, you can do that using the states if the Drawer and rendering its components based on that states. Idk, if this is ideal but it works. – Gui Herzog Sep 02 '17 at 14:49