简体   繁体   中英

How to do conditional rendering using switch in react native?

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!

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>
            )
        })}
    )
}

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>
   ))

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM