Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch Case Doesn't work in React Native

I am relatively new to JS and RN. But I have gotten my self stuck in this mess.

I am, in React Native, trying to call a rendering of a badge depicting ones "Rank" which will change depending on what the calling will use for id.

To do this, I am calling a js-file with a Switch in the function so that, depending on the id I call the Rank with, it will return different ones.

My code currently looks like this:

'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/[email protected]')} style={{padding: 10}} />
          </View>
          );
      case 'bigone':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/[email protected]')} style={{padding: 10}} />
          </View>
          );
      case 'smalltwo':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/[email protected]')} style={{padding: 10}} />
            <Image source={require('../img/[email protected]')} style={{padding: 10}} />
          </View>
          );
      case 'bigtwo':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/[email protected]')} style={{padding: 10}} />
            <Image source={require('../img/[email protected]')} 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;

And I call this simply by:

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

But it always returns the default. And I have tried out many different variations in declaring the variable and so on. But I have no idea on where I have gone wrong.

like image 215
Niklas Lindeke Avatar asked Feb 07 '23 05:02

Niklas Lindeke


1 Answers

The id is passed in props to the Rank Component. You need to access that 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/[email protected]')} style={{padding: 10}} />
          </View>
          );
      case 'bigone':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/[email protected]')} style={{padding: 10}} />
          </View>
          );
      case 'smalltwo':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/[email protected]')} style={{padding: 10}} />
            <Image source={require('../img/[email protected]')} style={{padding: 10}} />
          </View>
          );
      case 'bigtwo':
        return (
          <View style={styles.lvl2}>
            <Image source={require('../img/[email protected]')} style={{padding: 10}} />
            <Image source={require('../img/[email protected]')} 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;
like image 97
agenthunt Avatar answered Feb 10 '23 12:02

agenthunt