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}></Button>} rightButton={<Button style={styles.menuButton}></Button>} /> ); } });
Applying it on another page:
<NavigationBar title="Categories" titleColor="#ffffff" NavBarColor="#f0b210"/>
How to do this properly? Thanks in advance.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With