切换案例在React Native中不起作用

Nik*_*eke 3 javascript switch-statement react-native

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

在React Native中,我试图调用徽章的渲染来描绘"Rank",这将根据调用将用于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;
Run Code Online (Sandbox Code Playgroud)

我简单地称之为:

var Rank = require('../Components/Rank')
.
.
.
<Rank id={'smallone'} />
Run Code Online (Sandbox Code Playgroud)

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

age*_*unt 8

id通过props传递给Rank Component.你需要访问它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;
Run Code Online (Sandbox Code Playgroud)