Reuse code in react native

0

I have a code that repeats on every page.

How can I solve this problem by reusing the script.

export default class App extends Component<{}> {
   onMenuItemSelected = item =>{
      if(item == "inicial")
      {
         Actions.inicial();
      }
      else if(item == "agendamento")
      {
         Actions.agendamento();
      }
   }

   render() {
      const menu = <Menu onItemSelected={this.onMenuItemSelected} />;
      return (
         ...
      );
   }
}

In case I would like to reuse the code of onMenuItemSelected .

    
asked by anonymous 17.12.2017 / 00:28

1 answer

0

One way is to create your own component for the Menu:

class MeuMenu extends Component {
   onMenuItemSelected = item =>{
      if(item == "inicial")
      {
         Actions.inicial();
      }
      else if(item == "agendamento")
      {
         Actions.agendamento();
      }
   }

   render() {
      return <Menu onItemSelected={this.onMenuItemSelected} />;
   }
}

And use it anywhere:

export default class App extends Component<{}> {
   render() {
      const menu = <MeuMenu />;
      return (
         ...
      );
   }
}
    
20.12.2017 / 18:38