4

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.

1
  • switch (this.id) { Commented Apr 18, 2016 at 12:23

1 Answer 1

8

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe its a silly question but don't we need to use also break?
If we didn't use a return in each case and returned at the end of the function, then break is needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.