Abhishek Patil
Abhishek Patil

Reputation: 542

How to pass parameters to dynamic screen in React Native?

Assume I have several screens as A,B,C,D..... I am going to conditionally render these screens and need to pass some same params to all of them. Can I pass a variable to them after deciding which one to render? for now, I can do,

let screen;
if(condition1) screen = <A {params}/> 
if(condition2) screen = <B {params}/>
if(condition3) screen = <C {params}/>
.
.
return screen

In this approach, my code is repeating what I want

if(condition1) screen = A
if(condition2) screen = B
if(condition3) screen = C

return <screen {params} /> 

but we cant render a screen like this <screen/> coz its a variable, not the component.

Is there a way I can achieve this so I don't have to repeat the code to improve the scalability

Upvotes: 0

Views: 140

Answers (1)

jnpdx
jnpdx

Reputation: 52545

You can use React.createElement to create the component from the dynamically-chosen view:

if(condition1) screen = A;
if(condition2) screen = B;
if(condition3) screen = C;
const component = React.createElement(screen,params);
return component;

Upvotes: 1

Related Questions