react-native - 无法理解我的应用程序上的 React-native 错误
问题描述
我一直在学习使用 react-native 的 udemy 教程。我遇到了这个错误,如 iphone 图像所示。我的应用程序的代码如下:当我转到应用程序上显示错误的此文件时,会发生此问题
import React, { useReducer } from 'react' ;
import {View, StyleSheet, Text, FlatList} from 'react-native';
import ColorCounter from '../components/ColorCounter';
const COLOUR_COUNT = 15 ;
const reducer = (state, action) => {
switch (action.colorToChange) {
case 'red':
return { ...state, red: state.red + action.amount };
case 'green':
return { ...state, green: state.green + action.amount };
case 'blue':
return { ...state, blue: state.blue + action.amount };
default:
return state ;
}
};
const SquareScreen = () => {
const [state, dispatch] = useReducer(reducer, {red: 0, green: 0, blue: 0}) ;
const [red, green, blue] = state ;
return ( <View>
<ColorCounter
onIncrease={() =>
dispatch ({ colorToChange: 'red', amount: COLOUR_COUNT}) }
onDecrease={() =>
dispatch ({ colorToChange: 'red', amount: -1 * COLOUR_COUNT }) }
color="Red"
/>
<ColorCounter
onIncrease={() =>
dispatch ({ colorToChange: 'blue', amount: COLOUR_COUNT}) }
onDecrease={() =>
dispatch ({ colorToChange: 'blue', amount: -1 * COLOUR_COUNT })}
color="Blue"/>
<ColorCounter
onIncrease={() =>
dispatch ({ colorToChange: 'green', amount: COLOUR_COUNT})}
onDecrease={() =>
dispatch ({ colorToChange: 'green', amount: -1 * COLOUR_COUNT })}
color="Green" />
<View
style={{
height: 150,
width: 150,
backgroundColor: `rgb(${red}, ${green}, ${blue})`
}}
/>
</View>
);
};
const styles = StyleSheet.create({}) ;
export default SquareScreen;
当我转到此代码时出现的错误消息图像如下:
解决方案
纠正这个:
const [state, dispatch] = useReducer(reducer, {red: 0, green: 0, blue: 0}) ;
const {red, green, blue} = state ;
第一行是一个数组解构,第二行是一个对象
推荐阅读
- c++ - 处理具有多种构建类型的介子构建选项
- sql - 加入表格时遇到问题
- php - 如何从图像的字节字符串将图像保存在上传文件夹中?
- ios - 如何将多个过滤器应用于视频的特定部分
- android - Android HTTP Post 请求在后台
- c# - 如何解决此错误 - 任何人都可以提供解决方案吗?
- javascript - 像“http://test: ”这样的东西没有显示在 IE 的文本区域中
- apache-spark - Pyspark:如何在计算 pyspark sql 函数时排除列的非空记录?
- python - 如何使用python为json文件中的键添加值
- sql-server - 使用 PATINDEX 对字符串搜索进行性能调整。