【问题标题】:ToolbarAndroid react native dynamic subtitle bind to stateToolbarAndroid react原生动态字幕绑定到状态
【发布时间】:2016-11-04 00:01:03
【问题描述】:

我正在尝试根据状态变量设置“ToolbarAndroid”的字幕,但没有成功,

我将初始状态设置为 null 并且我正在尝试执行类似“仅在不为 null 时显示字幕”之类的操作

  constructor(props) {
    super(props);

    this.state = {
      subtitle: null
    }
  }

  render() { ...

  <ToolbarAndroid
   ..
        subtitle={() => {
          if (this.state.subtitle !== null) {
           return this.state.subtitle;
          } else {
           return '';
          }
        }}

我错过了什么?

【问题讨论】:

    标签: javascript reactjs react-native ecmascript-6


    【解决方案1】:

    我猜你想导出这个组件。如果是,您可以将默认道具设置为字幕的值。 例如,

    static defaultProps = {
      subtitle: ''
    };
    
    static propTypes = {
       subtitle: PropTypes.string
    };
    

    所以,在字幕的道具上,你就这样写吧,

    subtitle={this.props.subtitle}
    

    当你想使用这个组件的时候,只要传一个值就可以了~

    【讨论】:

      【解决方案2】:

      我已经设法解决了这个问题

        subtitle={(this.state.subtitle !== null) ? this.state.subtitle : '' }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-08
        • 1970-01-01
        • 2017-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多