变量未定义,何时定义?

时间:2019-06-19 07:10:32

标签: javascript reactjs react-native expo

因此,有些上下文是它从firebase中拉出了本地图像链接,然后应该在单击标记时将其设置为页面上的设置图像,图像值适用于标记,但在返回之外的视图上设置不能说它是一个未定义的变量,控制台日志也显示正确的值。我删除了所有不相关的代码部分,以使其看起来尽可能易于阅读。

export class HomeScreen extends Component {
  constructor(props) {
    super(props);

    this.state = {
      image: '',
    };
  }

  render() {
    return (
      <View style={styles.container}>
        <MapView>
          {this.state.data.map((obj, index) => {
            return (
              <MapView.Marker
                key={index}
                onPress={() => {
                  this.setState({ image: obj.image });
                  let { value } = 'require(' + obj.image + ')';
                  console.log(value);
                }}>
                <Image source={require(obj.image)} style={styles.pet} />
              </MapView.Marker>
            );
          })}
        </MapView>

        <View style={styles.petinfo}>
          <Image style={styles.locpetimg} source={value} />
        </View>
      </View>
    );
  }
}

export default withNavigation(HomeScreen);

2 个答案:

答案 0 :(得分:0)

export class HomeScreen extends Component {
  constructor(props) {
    super(props);

    this.state = {
      image: '',
      data:[]
    };
  }

  render() {
    return (
      <View style={styles.container}>
        <MapView>
          {this.state.data.map((obj, index) => {
            return (
              <MapView.Marker
                key={index}
                onPress={() => {
                  this.setState({ image: obj.image });
                  let { value } = 'require(' + obj.image + ')';
                  console.log(value);
                }}>
                <Image source={require(obj.image)} style={styles.pet} />
              </MapView.Marker>
            );
          })}
        </MapView>

        <View style={styles.petinfo}>
          <Image style={styles.locpetimg} source={value} />
        </View>
      </View>
    );
  }
}

export default withNavigation(HomeScreen);

答案 1 :(得分:0)

您是否要在<Image/>内使用通过“ onPress”设置的“值”?这是不可能的,因为它超出范围。尝试类似

export class HomeScreen extends Component {
  constructor(props) {
    super(props);
    this.value = null;
    this.state = {
      image: '',
    };
  }

  render() {
    return (
      <View style={styles.container}>
        <MapView>
          {this.state.data.map((obj, index) => {
            return (
              <MapView.Marker
                key={index}
                onPress={() => {
                  this.setState({ image: obj.image });
                  this.value = 'require(' + obj.image + ')';
                  console.log(value);
                }}>
                <Image source={require(obj.image)} style={styles.pet} />
              </MapView.Marker>
            );
          })}
        </MapView>

        <View style={styles.petinfo}>
          <Image style={styles.locpetimg} source={this.value} />
        </View>
      </View>
    );
  }
}

export default withNavigation(HomeScreen);

在这里,我将值存储在类的成员变量中,然后使用它。