Force rerendering Hamburger icon when clicking on button on SideMenu












0















When a user click on a page button in the SideMenu, the drawer is closed and the navigation happens.



The HamburgerMenu component has an internal state that contains whether it should be an hamburger sign or an "X".



The problem is that the HamburgerMenu is not aware of this navigation (and thus not aware to the fact that the drawer was closed) so the icon is still "X" when the drawer is closed by the user's click.



I can't find a way to make the SideMenu notify HamburgerMenu about the change/event.



Here is the relevant code from App.js:



const DrawerNavigator = createDrawerNavigator({
Home: {
screen: HomeScreen
},
Settings: {
screen: SettingsScreen
}
}, {
initialRouteName: 'Home',
contentComponent: SideMenu
});

const AppNavigator = createStackNavigator({
DrawerNavigator: {
screen: DrawerNavigator,
navigationOptions: ({ navigation }) => ({
headerLeft: <HamburgerMenu navigation={navigation} />
})
}
});

const AppContainer = createAppContainer(AppNavigator);
export default AppContainer;


EDIT:
Here is my HamburgerMenu code:



export default class HamburgerMenu extends Component {
constructor(props){
super(props);
this.state={
active: false
}
}

onPress(){
let newActive = !this.state.active;
this.setState({active: newActive});
if(newActive){
this.props.navigation.dispatch(DrawerActions.openDrawer())
}
else {
this.props.navigation.dispatch(DrawerActions.closeDrawer())
}
}

render(){
return (
<Hamburger active={this.state.active} type="spinCross" onPress={this.onPress.bind(this)} />
);
}
}


Thanks!










share|improve this question





























    0















    When a user click on a page button in the SideMenu, the drawer is closed and the navigation happens.



    The HamburgerMenu component has an internal state that contains whether it should be an hamburger sign or an "X".



    The problem is that the HamburgerMenu is not aware of this navigation (and thus not aware to the fact that the drawer was closed) so the icon is still "X" when the drawer is closed by the user's click.



    I can't find a way to make the SideMenu notify HamburgerMenu about the change/event.



    Here is the relevant code from App.js:



    const DrawerNavigator = createDrawerNavigator({
    Home: {
    screen: HomeScreen
    },
    Settings: {
    screen: SettingsScreen
    }
    }, {
    initialRouteName: 'Home',
    contentComponent: SideMenu
    });

    const AppNavigator = createStackNavigator({
    DrawerNavigator: {
    screen: DrawerNavigator,
    navigationOptions: ({ navigation }) => ({
    headerLeft: <HamburgerMenu navigation={navigation} />
    })
    }
    });

    const AppContainer = createAppContainer(AppNavigator);
    export default AppContainer;


    EDIT:
    Here is my HamburgerMenu code:



    export default class HamburgerMenu extends Component {
    constructor(props){
    super(props);
    this.state={
    active: false
    }
    }

    onPress(){
    let newActive = !this.state.active;
    this.setState({active: newActive});
    if(newActive){
    this.props.navigation.dispatch(DrawerActions.openDrawer())
    }
    else {
    this.props.navigation.dispatch(DrawerActions.closeDrawer())
    }
    }

    render(){
    return (
    <Hamburger active={this.state.active} type="spinCross" onPress={this.onPress.bind(this)} />
    );
    }
    }


    Thanks!










    share|improve this question



























      0












      0








      0








      When a user click on a page button in the SideMenu, the drawer is closed and the navigation happens.



      The HamburgerMenu component has an internal state that contains whether it should be an hamburger sign or an "X".



      The problem is that the HamburgerMenu is not aware of this navigation (and thus not aware to the fact that the drawer was closed) so the icon is still "X" when the drawer is closed by the user's click.



      I can't find a way to make the SideMenu notify HamburgerMenu about the change/event.



      Here is the relevant code from App.js:



      const DrawerNavigator = createDrawerNavigator({
      Home: {
      screen: HomeScreen
      },
      Settings: {
      screen: SettingsScreen
      }
      }, {
      initialRouteName: 'Home',
      contentComponent: SideMenu
      });

      const AppNavigator = createStackNavigator({
      DrawerNavigator: {
      screen: DrawerNavigator,
      navigationOptions: ({ navigation }) => ({
      headerLeft: <HamburgerMenu navigation={navigation} />
      })
      }
      });

      const AppContainer = createAppContainer(AppNavigator);
      export default AppContainer;


      EDIT:
      Here is my HamburgerMenu code:



      export default class HamburgerMenu extends Component {
      constructor(props){
      super(props);
      this.state={
      active: false
      }
      }

      onPress(){
      let newActive = !this.state.active;
      this.setState({active: newActive});
      if(newActive){
      this.props.navigation.dispatch(DrawerActions.openDrawer())
      }
      else {
      this.props.navigation.dispatch(DrawerActions.closeDrawer())
      }
      }

      render(){
      return (
      <Hamburger active={this.state.active} type="spinCross" onPress={this.onPress.bind(this)} />
      );
      }
      }


      Thanks!










      share|improve this question
















      When a user click on a page button in the SideMenu, the drawer is closed and the navigation happens.



      The HamburgerMenu component has an internal state that contains whether it should be an hamburger sign or an "X".



      The problem is that the HamburgerMenu is not aware of this navigation (and thus not aware to the fact that the drawer was closed) so the icon is still "X" when the drawer is closed by the user's click.



      I can't find a way to make the SideMenu notify HamburgerMenu about the change/event.



      Here is the relevant code from App.js:



      const DrawerNavigator = createDrawerNavigator({
      Home: {
      screen: HomeScreen
      },
      Settings: {
      screen: SettingsScreen
      }
      }, {
      initialRouteName: 'Home',
      contentComponent: SideMenu
      });

      const AppNavigator = createStackNavigator({
      DrawerNavigator: {
      screen: DrawerNavigator,
      navigationOptions: ({ navigation }) => ({
      headerLeft: <HamburgerMenu navigation={navigation} />
      })
      }
      });

      const AppContainer = createAppContainer(AppNavigator);
      export default AppContainer;


      EDIT:
      Here is my HamburgerMenu code:



      export default class HamburgerMenu extends Component {
      constructor(props){
      super(props);
      this.state={
      active: false
      }
      }

      onPress(){
      let newActive = !this.state.active;
      this.setState({active: newActive});
      if(newActive){
      this.props.navigation.dispatch(DrawerActions.openDrawer())
      }
      else {
      this.props.navigation.dispatch(DrawerActions.closeDrawer())
      }
      }

      render(){
      return (
      <Hamburger active={this.state.active} type="spinCross" onPress={this.onPress.bind(this)} />
      );
      }
      }


      Thanks!







      react-native react-navigation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 8 at 21:17







      user3343396

















      asked Jan 1 at 19:32









      user3343396user3343396

      125




      125
























          1 Answer
          1






          active

          oldest

          votes


















          0














          there is no event given for drawer as now but you can make your own by determining Whether drawer is open or close



          const parent = this.props.navigation.dangerouslyGetParent();
          const isDrawerOpen = parent && parent.state && parent.state.isDrawerOpen;


          so you can maintain the state of the menu click inside the HamburgerMenu



          or you can listen to the action from redux as well that is bit complex






          share|improve this answer
























          • This check will not help because the Hamburger component is not rendered. Where do you suggest i should put it? see Hamburger component code in my post edit

            – user3343396
            Jan 8 at 21:15











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998336%2fforce-rerendering-hamburger-icon-when-clicking-on-button-on-sidemenu%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          there is no event given for drawer as now but you can make your own by determining Whether drawer is open or close



          const parent = this.props.navigation.dangerouslyGetParent();
          const isDrawerOpen = parent && parent.state && parent.state.isDrawerOpen;


          so you can maintain the state of the menu click inside the HamburgerMenu



          or you can listen to the action from redux as well that is bit complex






          share|improve this answer
























          • This check will not help because the Hamburger component is not rendered. Where do you suggest i should put it? see Hamburger component code in my post edit

            – user3343396
            Jan 8 at 21:15
















          0














          there is no event given for drawer as now but you can make your own by determining Whether drawer is open or close



          const parent = this.props.navigation.dangerouslyGetParent();
          const isDrawerOpen = parent && parent.state && parent.state.isDrawerOpen;


          so you can maintain the state of the menu click inside the HamburgerMenu



          or you can listen to the action from redux as well that is bit complex






          share|improve this answer
























          • This check will not help because the Hamburger component is not rendered. Where do you suggest i should put it? see Hamburger component code in my post edit

            – user3343396
            Jan 8 at 21:15














          0












          0








          0







          there is no event given for drawer as now but you can make your own by determining Whether drawer is open or close



          const parent = this.props.navigation.dangerouslyGetParent();
          const isDrawerOpen = parent && parent.state && parent.state.isDrawerOpen;


          so you can maintain the state of the menu click inside the HamburgerMenu



          or you can listen to the action from redux as well that is bit complex






          share|improve this answer













          there is no event given for drawer as now but you can make your own by determining Whether drawer is open or close



          const parent = this.props.navigation.dangerouslyGetParent();
          const isDrawerOpen = parent && parent.state && parent.state.isDrawerOpen;


          so you can maintain the state of the menu click inside the HamburgerMenu



          or you can listen to the action from redux as well that is bit complex







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 5:45









          JigarJigar

          731316




          731316













          • This check will not help because the Hamburger component is not rendered. Where do you suggest i should put it? see Hamburger component code in my post edit

            – user3343396
            Jan 8 at 21:15



















          • This check will not help because the Hamburger component is not rendered. Where do you suggest i should put it? see Hamburger component code in my post edit

            – user3343396
            Jan 8 at 21:15

















          This check will not help because the Hamburger component is not rendered. Where do you suggest i should put it? see Hamburger component code in my post edit

          – user3343396
          Jan 8 at 21:15





          This check will not help because the Hamburger component is not rendered. Where do you suggest i should put it? see Hamburger component code in my post edit

          – user3343396
          Jan 8 at 21:15




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998336%2fforce-rerendering-hamburger-icon-when-clicking-on-button-on-sidemenu%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules