【问题标题】:FAB not working in React NativeFAB 在 React Native 中不起作用
【发布时间】:2018-06-27 09:19:20
【问题描述】:
import React, { Component } from 'react';
import { Container, Header, View, Button, Icon, Fab } from 'native-base';
export default class FABExample extends Component {
  constructor() {
    this.state = {
      active: 'true'
    };
  }
  render() {
    return (  
      <Container>
        <Header />
        <View style={{ flex: 1 }}>
          <Fab
            active={this.state.active}
            direction="up"
            containerStyle={{ }}
            style={{ backgroundColor: '#5067FF' }}
            position="bottomRight"
            onPress={() => this.setState({ active: !this.state.active })}>
            <Icon name="share" />
            <Button style={{ backgroundColor: '#34A34F' }}>
              <Icon name="logo-whatsapp" />
            </Button>
            <Button style={{ backgroundColor: '#3B5998' }}>
              <Icon name="logo-facebook" />
            </Button>
            <Button disabled style={{ backgroundColor: '#DD5144' }}>
              <Icon name="mail" />
            </Button>
          </Fab>
        </View>
      </Container>
    );
  }
}

我正在使用上面的代码在我的项目中添加一个 FAB。但我收到一个错误:

正文:{"type":"TransformError","sn-p":" 3| constructor() { 4| this.state ......

【问题讨论】:

  • this.state {...}之前添加super();

标签: reactjs react-native floating-action-button


【解决方案1】:

这似乎是 NativeBase 给出的示例代码中的错误。为了纠正错误,请在您的构造函数中添加 super();,如下所示。

constructor() {
  super();
  this.state = {
    active: 'true'
  };
}

您可以参考此link 了解更多详细信息,为什么构造函数需要在访问 'this' 之前调用 super

【讨论】:

  • 谢谢.. 现在错误消失了。但默认显示子图标。
  • 你能告诉我子图标是什么意思吗?如果您指的是 facebook、whatsapp 共享图标,我可以看到您共享的 sn-p 中编写的代码。你能澄清一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-01
  • 2020-06-04
  • 2020-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多