Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Parameters to Components in React Native

Tags:

I'm trying to use a common Navigation Component I made in React-Native. At the point of calling I want to set the Navigation Bar Tint, Title etc.

Nav Bar Code:

var NavigationBar = React.createClass({     render: function(title, titleColor, NavBarColor) {         var titleConfig = {             title: title,             tintColor: titleColor,         };          return (             <NavBar                 title={titleConfig}                 tintColor={NavBarColor}                 leftButton={<Button style={styles.menuButton}>&#xf0c9;</Button>}                 rightButton={<Button style={styles.menuButton}>&#xf07a;</Button>} />         );     } }); 

Applying it on another page:

<NavigationBar title="Categories" titleColor="#ffffff" NavBarColor="#f0b210"/> 

How to do this properly? Thanks in advance.

like image 537
Nimila Hiranya Avatar asked Dec 01 '15 04:12

Nimila Hiranya


People also ask

How do you pass state value to another component react?

Sending state/props to another component using the onClick event: So first we store the state/props into the parent component i.e in which component where we trigger the onClick event. Then to pass the state into another component, we simply pass it as a prop.


1 Answers

First off render does not take any parameters, what you want to do is to reference your props that you passed in.

render: function () {   var titleConfig = {       title: this.props.title,       tintColor: this.props.titleColor   };     // Rest of code } 

Just by doing this, whenever your NavigationBar rerenders so will the NavBar component too.

A super simple example demonstrating this

var NavBar = React.createClass({   render: function () {     return <div id="navbar" style={{backgroundColor: this.props.tintColor}}>     <h1 style={{color: this.props.title.tintColor}}>{this.props.title.title}</h1>     </div>;   } });  var NavigationBar = React.createClass({     render: function() {         var titleConfig = {             title: this.props.title,             tintColor: this.props.titleColor,         };          return (             <NavBar                 title={titleConfig}                 tintColor={this.props.NavBarColor}                 />         );     } });   React.render(<NavigationBar title="Categories" titleColor="#ff0" NavBarColor="#f0b210" />, document.body); 
like image 171
Henrik Andersson Avatar answered Oct 14 '22 03:10

Henrik Andersson