13

Ok i got components imported as

import Payment from './pages/payment';
import Chat from './pages/chat';

Now I am using Drawer component and using it together with Navigator my renderScene become something like this

if( route.id == 'payment'){
    return  <Drawer  xx={} yy={} and a lot more >
              <Payment navigator={navigator} />
            </Drawer>
}

if(route.id == 'chat'){
    return <Drawer  xx={} yy={} and a lot more >
              <Chat navigator={navigator} />
            </Drawer>
}

Those lengthy Drawer code are being used again and again. I want to store that <Payment navigator={navigator} > or the other into a variable and then return that with Drawer only once.

How can i store it and return it with Drawer?

Thanks

3 Answers 3

10

Not sure if you are asking this but what about something like:

const routes = {
 payment: Payment,
 chat: Chat
 ...
}

And then, just:

const Scene = routes[route.id];
return (
  <Drawer>
    <Scene navigator={navigator}/>
  </Drawer>
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you. I actually tried very similar to this before but was using var instead of const and it was giving me error. Changed to const and it work :)
2

Here you have 3 options:

// 1. Group the drawer props in an object
const drawerProps = {
  xx: ...,
  yy: ...
};
<Drawer {...drawerProps}>
  <Chat navigator={navigator} />    
</Drawer>

// 2. Define a wrapper object that populates the common Drawer props
const CustomDrawer = ({ children }) => (
  <Drawer  xx={} yy={} and a lot more>
    {children}
  </Drawer>
);


// 3. Define a wrapper object that populates the common Drawer props with default props. (Can be 
// overriden.)
const CustomDrawer = ({ 
  xx='XX',
  yy='YY',
  children 
}) => (
  <Drawer  xx={xx} yy={yy} and a lot more>
    {children}
  </Drawer>
);

EDIT: I missunderstood your question, for storing the inner part you just have to assign it to a varible and use it.

const routes = {
  chat: <Chat navigator={navigator} />,
  payment: <Payment navigator={navigator} />,  
}


<Drawer {...drawerProps}>
  { routes[route.id] }
</Drawer>

1 Comment

Thanks , very similar approach to accepted answer :)
0

I propose this solution with a React hook (React v16.8+).

The useMemo returns a component according to the route agument passed to the switch. The useMemo is updated each time one of the internal variables (passed as a second argument as route) is updated.

import React, { useState, useMemo } from 'react';

export default function App ({
    route,
    navigator
}) {
    const [route, setRoute] = useState('payment');

    const mainContent = useMemo(() => {
        return () => {
            switch (route) {
                case 'payment':
                    return (
                        <Payment navigator={navigator} />
                    );
    
                case 'chat':
                    return (
                        <Chat navigator={navigator} />
                    );
            }
        }
    }, [route, navigator])
    
    return (
        <Drawer xx={} yy={} and a lot more >
            { mainContent() }
        </Drawer>
    );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.