1

Hello I'm trying to do switch statement in my project.I've an object image as follows

export const images = [
  {
    image: BASE.URL + 'Images/Plumber.png',

  },
  {
    image: BASE.URL + 'Images/electrician.png',


  },
 {
    image: BASE.URL + 'Images/ac.png',

  }
]

I'm fetching the list of workers from server and render it within a Card.So the server response only contains the name of workers.I'm trying to give images along with them.So I've written a switch statement but image is not coming along with the text.Following is my code.

    import { images } from './data';
    renderImage() {
        const { workType } = this.state;
        switch (workType) {
          case 'Plumber':
            return (
              <Image style={{ height: 100, width: 100 }} source={{ uri: images[0].image }} />
            );
          case 'Electrician':
            return (
              <Image style={{ height: 100, width: 100 }} source={{ uri: images[1].image }} />
            );
     case 'AC'
            return (
              <Image style={{ height: 100, width: 100 }} source={{ uri: images[2].image }} />
            );
        }
      }
   render(){
    const { workers,workType } = this.state;
    return(
    {workers.map((a, index) => (
                    <TouchableOpacity onPress={() => this.popUpDialog(a.id, a.work_type)}>
                      <Card>
                        {this.renderImage()}
                        <Text>{a.work_type}</Text>
                      </Card>
                    </TouchableOpacity>
              ))}
    )
    }

What wrong I'm doing please help me to find a solution.Thank you!

1

4 Answers 4

1

Try to console.log your this.state.workType because it might not be the value you think it should be. Since you dont have a default case, the function returns nothing.

Also, it could be easier if you take the workType as a parameter for your renderImage function. I suspect your this.state.workType will not be the same with your a.work_type in the workers.map function. You can do it like this

const renderImage = (workType) => {
  switch (workType) {
  ...
  }
}

//and then
   workers.map((a, index) => (
                    <TouchableOpacity onPress={() => this.popUpDialog(a.id, a.work_type)}>
                      <Card>
                        {this.renderImage(a.work_type)}
                        <Text>{a.work_type}</Text>
                      </Card>
                    </TouchableOpacity>
   ))
Sign up to request clarification or add additional context in comments.

Comments

1

I think you forgot to return the mapped component.

eg:

render() {
    const { workers,workType } = this.state;
    return(
        {workers.map((a, index) => {
            return(
                <TouchableOpacity onPress={() => this.popUpDialog(a.id, a.work_type)}>
                    <Card>
                        {this.renderImage()}
                        <Text>{a.work_type}</Text>
                    </Card>
                </TouchableOpacity>
            )
        })}
    )
}

Comments

0

My advice would be to go for a map and render the image in following way.

const WORKTYPE_TO_IMAGE = {
  Plumber: BASE.URL + 'Images/Plumber.png',
  Electrician: BASE.URL + 'Images/electrician.png',
  AC: BASE.URL + 'Images/ac.png',
};

renderImage(){
  return (
    <Image style={{ height: 100, width: 100 }} source={WORKTYPE_TO_IMAGE[this.state.workType]} />
  )
}

You were using Image tag thrice but now you'll only use it once (hence Do Not Repeat principle is followed). Furthermore, I really hate if/switch in my code because the requirements frequently, instead changing logic in your code, you should change only at one place (which is a constant in your case). The code will be really clean and short :)

2 Comments

So do I need to write a map method for WORKTYPE_TO_IMAGE ?
No, its just a wrapper object for all your work type image urls
0

I just give style to main view and then everything is working fine.

  renderImage(workType) {
    switch (workType) {
      case 0:
        return (
          <Image style={{ height: 100, width: 100, backgroundColor: 'green' }} />
        );
      case 1:
        return (
          <Image style={{ height: 100, width: 100, backgroundColor: 'yellow' }} />
        );
      case 2:
        return (
          <Image style={{ height: 100, width: 100, backgroundColor: 'blue' }} />
        );
    }
  }

  //source={{ uri: require('./1.jpg') }}
  render() {
    let workers = [{ work_type: 'Plumber' }, { work_type: 'Electrician' }, { work_type: 'AC' }];
    return (
      <View style={{ width: '100%', height: '100%', backgroundColor: 'red' }}>
        {
          workers.map((a, index) => (
            <TouchableOpacity onPress={() => this.popUpDialog(a.id, a.work_type)}>
              <View>
                {this.renderImage(index)}
                <Text>{a.work_type}</Text>
              </View>
            </TouchableOpacity>
          ))
        }
      </View>
    )
  }

I have set color as I don't have an image.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.