首页 > 解决方案 > 视图样式在 React Native 中不起作用

问题描述

在我的代码中使用之前,我是 React Native 的新手const Main = () => { ..}

但是当我更改export default class Main extends React.Component { ..}为时,会出现问题,例如 View 中的样式不起作用但错误',' expected in style

截图:

在此处输入图像描述

我正在使用新版本的 react native

标签: react-native

解决方案


您缺少应该使用 return() 方法的 render() 方法。

前任:

export default class LotsOfStyles extends Component {
  render() {
    return (
      <View>
        <Text style={styles.red}>just red</Text>
        <Text style={styles.bigBlue}>just bigBlue</Text>
        <Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text>
        <Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text>
      </View>
    );
  }
}

反应本机文档


推荐阅读