在本地反应中从解析的对象数组渲染图像源

时间:2016-06-15 16:45:01

标签: reactjs react-native ecmascript-6 babeljs jsx

我正在构建一个反应本机应用程序,它假设使用一些“元数据”对象作为源。我正在解析数组中的每个对象并为每个item返回一个JSX布局。我唯一的问题是如何从图像中提供源,因为我将它们存储在本地并且需要require("link")它们。我的代码给了我错误:

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

我的代码:

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

版本:

  "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 个答案:

答案 0 :(得分:7)

你可以做这样的事情

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