Render images sources from parsed array of objects in react native

我们两清 提交于 2020-02-08 03:15:46

问题


I'm building a react native application which suppose to use some 'meta-data' objects as sources. I'm parsing each object in array and returning a JSX layout for each item. The only problem I have is how to provide sources from images, since I have them stored locally and need to require("link") them. My code gives me error:

function loadModuleImplementation(moduleId, module) {
  moduleId = "../../../images/icons/photographer/Party.png"

My code:

class SelectScreen extends Component {
  props:Props;


  Details = [
        {
          "type": "Party",
          "image": "../../../images/icons/photographer/Party.png"
        },
        {
          "type": "Wedding",
          "image": "../../../images/icons/photographer/Wedding.png"
        },
        {
          "type": "Architecture",
          "image": "../../../images/icons/photographer/Arhitecture.png"
        },
        {
          "type": "Christening",
          "image": "../../../images/icons/photographer/Christening.png"
        }
  ];

  render() {
    let renderPhotoTypes = () => {
      let type = [];

      this.Details.map( ( item )=> {
        type.push(
          <View style={styles.imageSelectItem} key={item.type}>
            <TouchableWithoutFeedback>
              <View>
                <View style={styles.imageContainer}>
                  <Image style={styles.image} source={require(item.image)}/>
                </View>
                <Text style={styles.text}>{item.type}</Text>
              </View>
            </TouchableWithoutFeedback>
          </View>
        );
      } );

      return type;
    };

    return (
      <ScrollView style={styleGuide.containerMain}>
        <Text style={styleGuide.heading_1}>
          Select type
        </Text>

        <View style={styles.imageSelectContainer}>
          {renderPhotoTypes()}
        </View>
      </ScrollView>);
  }
}

Versions:

  "dependencies": {
    "axios": "^0.12.0",
    "babel-relay-plugin": "^0.9.0",
    "buffer": "^4.6.0",
    "color": "^0.11.1",
    "invariant": "^2.2.1",
    "react": "~15.0.2",
    "react-native": "^0.26.1",
    "react-native-button": "^1.6.0",
    "react-native-drawer": "^2.2.3",
    "react-native-fbsdk": "^0.2.1",
    "react-native-message-bar": "^1.6.0",
    "react-native-radio-buttons": "^0.11.0",
    "react-native-vector-icons": "^2.0.3",
    "react-redux": "^4.4.5",
    "react-relay": "^0.9.0",
    "redux": "^3.5.2",
    "redux-logger": "^2.6.1",
    "redux-persist": "^3.2.2",
    "redux-thunk": "^2.1.0"
  }

回答1:


You can do something like this

  Details = [
        {
          type: "Party",
          image: require("../../../images/icons/photographer/Party.png")
        },
        {
          type: "Wedding",
          image: require("../../../images/icons/photographer/Wedding.png")
        },
        {
          type: "Architecture",
          image: require("../../../images/icons/photographer/Arhitecture.png")
        },
        {
          type: "Christening",
          image: require("../../../images/icons/photographer/Christening.png")
        }
  ];

  render() {
    let renderPhotoTypes = () => {
      let type = [];

      this.Details.map( ( item )=> {
        type.push(
          <View style={styles.imageSelectItem} key={item.type}>
            <TouchableWithoutFeedback>
              <View>
                <View style={styles.imageContainer}>
                  <Image style={styles.image} source={item.image}/>
                </View>
                <Text style={styles.text}>{item.type}</Text>
              </View>
            </TouchableWithoutFeedback>
          </View>
        );
      } );

      return type;
    };


来源:https://stackoverflow.com/questions/37841236/render-images-sources-from-parsed-array-of-objects-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!