javascript - 如何修复 "Can' t 查找变量 : item"

标签 javascript react-native expo

我正在构建一个简单的图像列表应用程序,具有一些额外的功能,其中之一是下载图像功能。

但是,我不断收到“找不到变量项”错误。我该如何解决这个问题?

item.image

// ...

downloadImage = () => {
  const fileUri = `${FileSystem.documentDirectory}breathtaking.jpg`

  FileSystem
    .downloadAsync(item.image, fileUri)
    .then(({ uri }) => {
      console.log('Finished downloading to ', uri)
    })

  CameraRoll.saveToCameraRoll(fileUri, 'photo')
}

// ...
return (
// ...
 <Button
   icon='file-download'
   mode='contained'
   onPress={this.downloadImage}
   style={{ borderRadius: 0, width: '33.4%' }}
 />
// ...
);

更新

componentDidMount() {
    const url = "/* json */";
    this.setState({ isLoading: true });
    fetch(url)
      .then(response => response.json())
      .then(responseJson => {
        this.setState({
          dataSource: shuffle(responseJson.listings),
          dataBackup: responseJson.listings,
          isLoading: false
        });
      })
      .catch(error => {
        console.log(error);
      });
  }
renderItem = ({ item }) => {
    return (
      <View style={{ padding: 15, paddingBottom: 0 }}>
        <Card elevation={1}>
          <View
            style={{
              flex: 1,
              flexDirection: "row",
              flexWrap: "wrap",
              alignItems: "flex-start"
            }}
          >                  

            <View style={{ flex: 1 }}>
              <TouchableOpacity
                    onPress={() => {
                      this.toggleModal();
                      this.setState({
                        webViewurl: item.image
                      });
                    }}
                    onLongPress={() => Linking.openURL(item.image)}
                    activeColor="blue"
                  >
              <ImageBackground
                source={{ uri: item.image }}
                style={{ height: 216 }}
              >
                <IconButton
                  icon="favorite-border"
                  size={20}
                  color="#6200EE"
                  style={{ alignSelf: "flex-end" }}
                  onPress={this._savedAlert}
                />
              </ImageBackground>
              </TouchableOpacity>
            </View>
          </View>
        </Card>
      </View>
    );
}

最佳答案

您尚未声明变量item

// ...

downloadImage = () => {
  const item = {}
  const fileUri = `${FileSystem.documentDirectory}breathtaking.jpg`

  FileSystem
    .downloadAsync(item.image, fileUri)
    .then(({ uri }) => {
      console.log('Finished downloading to ', uri);
    })

  CameraRoll.saveToCameraRoll(fileUri, 'photo');
}

// ...

你可以做这样的事情

关于javascript - 如何修复 "Can' t 查找变量 : item",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58751980/

相关文章:

javascript - 为什么在我尝试替换 @mention 时正则表达式不起作用?

ios - 网络错误 - React Native iOS 模拟器

ios - React Native 中的 Fetch(或 Axios)不适用于 iOS 应用程序的博览会

react-native - ExpoKit 和 React Native 项目的区别

react-native - 如何识别一个 Expo Push Notification Token 所属的 Expo 项目?

javascript - 如何使用 JQuery 重复更改颜色?

javascript - JS根据条件过滤(如果存在)

javascript - React/prefer-stateless-function vs 类装饰器?

android - 使用嵌套导航进行深度链接(react-native)

button - react native : onPress: How can I toggle the button text and function call?