我现在开始学习React—Native. 所以我按照https://docs.expo.dev/tutorial/ 步学习. 但有时候代码不起作用. 我做了一个简单的ImagePicker项目. 这是我的代码.

App.js

import { StatusBar } from 'expo-status-bar';
import { useState } from 'react';
import { StyleSheet, View } from 'react-native';

import Button from './components/Button';
import ImageViewer from './components/ImageViewer';
import * as ImagePicker from 'expo-image-picker';

const PlaceholderImage = require('./assets/images/background-image.png');

export default function App() {
  const { selectedImage, setSelectedImage } = useState(null);

  const pickImageAsync = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      allowsEditing: true,
      quality: 1,
    });

    if (!result.canceled) {
      setSelectedImage(result.assets[0].uri);

    } else {
      alert('You did not select any image.');
    }
  };
  return (
    <View style={styles.container}>
      <View style={styles.imageContainer}>
        <ImageViewer
          placeholderImageSource={PlaceholderImage}
          selectedImage={selectedImage}
        />
      </View>
      <View style={styles.footerContainer}>
        <Button theme="primary" label="Choose a photo" onPress={pickImageAsync} />
        <Button label="Use this photo" />
      </View>
      <StatusBar style="auto" />
    </View>
  );
}


const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#25292e',
    alignItems: 'center',
  },
  imageContainer: {
    flex: 1,
    paddingTop: 58,
  },
  footerContainer: {
    flex: 1 / 3,
    alignItems: 'center',
  },
});

components/ImageViewer.js

import { StyleSheet, Image } from 'react-native';

export default function ImageViewer({ placeholderImageSource, selectedImage }) {
  const imageSource = selectedImage ? { uri: selectedImage } : placeholderImageSource;

  return (
    <Image source={imageSource} style={styles.image} />
  )
}


const styles = StyleSheet.create({
  image: {
    width: 320,
    height: 440,
    borderRadius: 18,
  },
});

components/Button.js

import { StyleSheet, View, Pressable, Text } from 'react-native';
import FontAwesome from "@expo/vector-icons/FontAwesome";

export default function Button({ label, theme, onPress}) {
    if (theme === "primary") {
      return (
        <View style={[styles.buttonContainer, { borderWidth: 4, borderColor: "#ffd33d", borderRadius: 18 }]}>
          <Pressable
            style={[styles.button, { backgroundColor: "#fff" }]}
            onPress={onPress}
          >
            <FontAwesome
              name="picture-o"
              size={18}
              color="#25292e"
              style={styles.buttonIcon}
            />
            <Text style={[styles.buttonLabel, { color: "#25292e" }]}>{label}</Text>
          </Pressable>
        </View>
      );
    }
  
    return (
      <View style={styles.buttonContainer}>
          <Pressable style={styles.button} onPress={() => alert('You pressed a button.')}>
            <Text style={styles.buttonLabel}>{label}</Text>
          </Pressable>
        </View>
    );
  }

const styles = StyleSheet.create({
  buttonContainer: {
    width: 320,
    height: 68,
    marginHorizontal: 20,
    alignItems: 'center',
    justifyContent: 'center',
    padding: 3,
  },
  button: {
    borderRadius: 10,
    width: '100%',
    height: '100%',
    alignItems: 'center',
    justifyContent: 'center',
    flexDirection: 'row',
  },
  buttonIcon: {
    paddingRight: 8,
  },
  buttonLabel: {
    color: '#fff',
    fontSize: 16,
  },
});

当我运行npm run web并单击按钮并 Select 图像时,出现此错误.

enter image description here

我怎么解决这个问题? 请帮帮我

我希望运行时没有任何错误.

推荐答案

您遇到的错误是由于错误使用useState在您的App.js文件.要解决这个问题,您需要使用方括号[]来进行数组解构.

替换这一行:

const { selectedImage, setSelectedImage } = useState(null);

有:

const [selectedImage, setSelectedImage] = useState(null);

Javascript相关问答推荐

如何提取Cypress中文本

如何获取转换字节的所有8位?

如何使用Echart 5.5.0创建箱形图

如何判断属于多个元素的属性是否具有多个值之一

zoom svg以适应圆

单击子元素时关闭父元素(JS)

将状态向下传递给映射的子元素

. NET中Unix时间转换为日期时间的奇怪行为

JQuery. show()工作,但. hide()不工作

使用i18next在React中不重新加载翻译动态数据的问题

Mongoose post hook在使用await保存时不返回Postman响应

查找最长的子序列-无法重置数组

如何避免页面第一次加载时由于CSS样式通过JavaScript更改而出现闪烁

未定义引用错误:未定义&Quot;而不是&Quot;ReferenceError:在初始化&Quot;之前无法访问';a';

我可以使用空手道用户界面来获取网页的当前滚动位置吗?

将Auth0用户对象存储在nextjs类型脚本的Reaction上下文中

如何在Java脚本中并行运行for或任意循环的每次迭代

我们是否可以在reactjs中创建多个同名的路由

Select 所有输入.值

我正在为我的单选按钮在HTML中设置一个值.使用Java脚本,我如何才能获得该值?