【问题标题】:How to do conditional rendering using switch in react native?如何在 React Native 中使用 switch 进行条件渲染?
【发布时间】:2019-08-18 19:52:32
【问题描述】:

您好,我正在尝试在我的项目中执行 switch 语句。我有一个对象图像如下

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

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


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

  }
]

我正在从服务器获取工作人员列表并将其呈现在 Card 中。因此服务器响应仅包含工作人员的名称。我正在尝试将图像与他们一起提供。所以我写了一个switch 语句,但图像没有与文本一起出现。以下是我的代码。

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

我做错了什么,请帮助我找到解决方案。谢谢!

【问题讨论】:

标签: javascript reactjs react-native switch-statement conditional-rendering


【解决方案1】:

尝试使用console.log 你的this.state.workType,因为它可能不是你认为应该的值。由于您没有 default 案例,因此该函数不返回任何内容。

此外,如果您将workType 作为renderImage 函数的参数,可能会更容易。我怀疑您的this.state.workType 与workers.map 函数中的a.work_type 不同。你可以这样做

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

【讨论】:

    【解决方案2】:

    我想你忘了返回映射的组件。

    例如:

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

    【讨论】:

      【解决方案3】:

      我的建议是使用地图并以下列方式渲染图像。

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

      您曾三次使用图像标签,但现在您只会使用一次(因此遵循不重复原则)。此外,我真的很讨厌我的代码中的 if/switch,因为这些需求经常发生,而不是更改代码中的逻辑,您应该只在一个地方进行更改(在您的情况下这是一个常量)。代码将非常干净和简短:)

      【讨论】:

      • 那我需要为WORKTYPE_TO_IMAGE 写一个map方法吗?
      • 不,它只是您所有工作类型图像 url 的包装对象
      【解决方案4】:

      我只是给主视图赋予样式,然后一切正常。

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

      我已经设置了颜色,因为我没有图像。

      【讨论】:

        猜你喜欢
        • 2020-09-20
        • 2021-07-18
        • 2019-08-31
        • 1970-01-01
        • 2020-01-15
        • 1970-01-01
        • 2020-12-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多