【问题标题】:Switch Case Doesn't work in React NativeSwitch Case 在 React Native 中不起作用
【发布时间】:2016-04-18 12:21:25
【问题描述】:

我对 JS 和 RN 比较陌生。但是我把自己困在了这个烂摊子里。

我在 React Native 中尝试调用一个徽章的渲染来描绘“等级”,这将根据调用将用于 id 的内容而改变。

为此,我在函数中调用一个带有 Switch 的 js 文件,这样,根据我调用 Rank 时使用的 id,它会返回不同的。

我的代码目前如下所示:

'use strict';
import React, {
  StyleSheet,
  View,
  Text,
  ScrollView,
  Image,
  TouchableOpacity,
} from 'react-native';

var colors = require('../Styles/colorscheme');
import {Actions} from 'react-native-router-flux';

var id = this.id;
var Rank = React.createClass({
  render: function() {
    switch (id) {
      case 'smallone':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/full_star_small@10x.png')} style={{padding: 10}} />
          </View>
          );
      case 'bigone':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/full_star@10x.png')} style={{padding: 10}} />
          </View>
          );
      case 'smalltwo':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/full_star_small@10x.png')} style={{padding: 10}} />
            <Image source={require('../img/full_star_small@10x.png')} style={{padding: 10}} />
          </View>
          );
      case 'bigtwo':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/full_star@10x.png')} style={{padding: 10}} />
            <Image source={require('../img/full_star@10x.png')} style={{padding: 10}} />
          </View>
          );
      default:
        return (
          <View style={styles.lvl2}>
            <Text>Null</Text>
          </View>
          );
      }
    }
  });

var styles = StyleSheet.create({
  lvl2: {
    flexDirection: 'row',
    backgroundColor: colors.General.background,
    justifyContent: 'center',
    alignItems: 'center',
  },
  lvl1: {
    padding: 10,
    flexDirection: 'row',
    backgroundColor: colors.General.hardline,
    justifyContent: 'center',
    alignItems: 'center',
  },  
});

module.exports = Rank;

我只是这样称呼它:

var Rank = require('../Components/Rank')
.
.
.
<Rank id={'smallone'} />

但它总是返回默认值。我在声明变量等方面尝试了许多不同的变体。但我不知道我哪里出错了。

【问题讨论】:

  • switch (this.id) {

标签: javascript switch-statement react-native


【解决方案1】:

id 在 props 中传递给 Rank 组件。你需要访问那个this.props.id

'use strict';
import React, {
  StyleSheet,
  View,
  Text,
  ScrollView,
  Image,
  TouchableOpacity,
} from 'react-native';

var colors = require('../Styles/colorscheme');
import {Actions} from 'react-native-router-flux';

var id = this.id;
var Rank = React.createClass({
  render: function() {
    switch (this.props.id) {
      case 'smallone':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/full_star_small@10x.png')} style={{padding: 10}} />
          </View>
          );
      case 'bigone':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/full_star@10x.png')} style={{padding: 10}} />
          </View>
          );
      case 'smalltwo':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/full_star_small@10x.png')} style={{padding: 10}} />
            <Image source={require('../img/full_star_small@10x.png')} style={{padding: 10}} />
          </View>
          );
      case 'bigtwo':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/full_star@10x.png')} style={{padding: 10}} />
            <Image source={require('../img/full_star@10x.png')} style={{padding: 10}} />
          </View>
          );
      default:
        return (
          <View style={styles.lvl2}>
            <Text>Null</Text>
          </View>
          );
      }
    }
  });

var styles = StyleSheet.create({
  lvl2: {
    flexDirection: 'row',
    backgroundColor: colors.General.background,
    justifyContent: 'center',
    alignItems: 'center',
  },
  lvl1: {
    padding: 10,
    flexDirection: 'row',
    backgroundColor: colors.General.hardline,
    justifyContent: 'center',
    alignItems: 'center',
  },  
});

module.exports = Rank;

【讨论】:

  • 也许这是一个愚蠢的问题,但我们不需要也使用 break 吗?
  • 如果我们没有在每种情况下都使用return,而是在函数结束时返回,那么就需要break。
猜你喜欢
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多