javascript – 将参数传递给React Native中的组件

我试图使用我在React-Native中制作的通用导航组件.在呼叫点我想设置导航栏色调,标题等.

导航条码:

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>} />
        );
    }
});

将其应用于另一页面

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

如何正确地做到这一点?提前致谢.

解决方法

首先,渲染没有任何参数,你想做的是引用你传入的道具.
render: function () {
  var titleConfig = {
      title: this.props.title,tintColor: this.props.titleColor
  };  
  // Rest of code
}

只要这样做,每当您的NavigationBar都会重新投放NavBar组件.

一个超级简单的例子证明了这一点

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);

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...