【问题标题】:Syntax Error: Unexpected token, expected " , "语法错误:意外标记,应为“,”
【发布时间】:2021-09-03 18:19:39
【问题描述】:

我在 react native 中遇到了这个语法错误: “语法错误:意外的标记,应为”,“”

return(
    <View style={ gStyle.main }>
      <Modal visible = { modalWindow }>
        <View style={ gStyle.main }>
          <Ionicons name="close" size={30} color="black" style = {gStyle.iconClose} onPress = {() => setModalWindow(false)}/>
          <Text style = {gStyle.iconTitle}>Добавить статью</Text>
          <Form />
        </View>
      </Modal>
      <Ionicons name="add" size={34} color="black" style = {gStyle.iconAdd} onPress = {() => setModalWindow(true)}/>
      <Text style = { [gStyle.title, styles.head]}>Лента</Text>
      <FlatList data = {news} renderItem = {({item}) => (
        <TouchableOpacity style = {styles.item} onPress = {() => navigation.navigate('FullInfo', item )}>
          <Image style = {gStyle.image} source = {{uri: item.img}}/>
          <Text style = {styles.title}>{item.name}</Text>
          <Text style = {styles.anons}>{item.anons}</Text>
      </TouchableOpacity>
      )} />
    </View>
  );
}

我也不明白错误信息,当它说“预期”时,它到底是什么意思?

Error

更新

import React, {useState} from 'react';
import { StyleSheet, View, Text, TouchableOpacity, FlatList, Image, Modal } from 'react-native';
import { gStyle } from '../styles/style';
import { Ionicons } from '@expo/vector-icons';
import Form from './Form';

export default function Main({ navigation }) {

  const [news, setNews] = useState([
    {name: 'Google', anons: 'Something interesting', full: 'Google Fulling page', key: '1', img: 'https://static.zara.net/photos///2020/I/0/1/p/4341/757/800/2/w/1126/4341757800_6_2_1.jpg'},
    {name: 'Yahoo', anons: 'Something didn`t interesting', full: 'Yahoo Fulling page', key: '2', img: 'https://i.pinimg.com/564x/89/1d/83/891d833d89de653904500547b6257218.jpg'},
    {name: 'Yandex', anons: 'Something bake interesting', full: 'Yandex Fulling page', key: '3', img:'http://ideaswood.selbermachendeko.com/wp-content/uploads/2019/10/8-einladende-coole-Ideen-Holzbearbeitungskuche-Tiny-House-Holzbearbeitungsschreib-…-WoodWorking.jpg'}
  ]);

  const [modalWindow, setModalWindow] = useState(false); 

  const addArticle = (article => {
    setNews((list) => {
      article.key = Math.random().toString();
      return [
        article,
        ...list,
      ]
    });
    setModalWindow(false);
  }

  return(
    <View style={ gStyle.main }>
      <Modal visible = { modalWindow }>
        <View style={ gStyle.main }>
          <Ionicons name="close" size={30} color="black" style = {gStyle.iconClose} onPress = {() => setModalWindow(false)}/>
          <Text style = {gStyle.iconTitle}>Добавить статью</Text>
          <Form />
        </View>
      </Modal>
      <Ionicons name="add" size={34} color="black" style = {gStyle.iconAdd} onPress = {() => setModalWindow(true)}/>
      <Text style = { [gStyle.title, styles.head]}>Лента</Text>
      <FlatList data = {news} renderItem = {({item}) => (
        <TouchableOpacity style = {styles.item} onPress = {() => navigation.navigate('FullInfo', item )}>
          <Image style = {gStyle.image} source = {{uri: item.img}}/>
          <Text style = {styles.title}>{item.name}</Text>
          <Text style = {styles.anons}>{item.anons}</Text>
      </TouchableOpacity>
      )} />
    </View>
  );
}


const styles = StyleSheet.create({
  head: {
    marginBottom: 5,
  },
  item: {
    width: '100%',
    marginBottom: 10,
  },
  title: {
    fontFamily: 'mn-bold',
    fontSize: 22,
    textAlign: 'center',
    marginTop: 10,
    color: '#702573',
  },
  anons: {
    fontFamily: 'mn-regular',
    fontSize: 16,
    textAlign: 'center',
    marginTop: 5,
    color: '#111111',
  }
});

【问题讨论】:

  • 您能粘贴此返回语句上方的代码吗? ?
  • 错误出现在哪一行?
  • 已更新完整代码
  • 返回行错误 (28:2)

标签: javascript ios reactjs react-native jsx


【解决方案1】:

删除 article 参数的左括号,或添加右括号。

const addArticle = article => {
  setNews((list) => {
    article.key = Math.random().toString();
    return [article, ...list];
  });
  setModalWindow(false);
}

const addArticle = (article) => {
  setNews((list) => {
    article.key = Math.random().toString();
    return [article, ...list];
  });
  setModalWindow(false);
}

QnA

我也不明白错误信息,它到底是做什么的 意思是当它说“预期”时?

语法方面,我假设它(linter?)假设您正在尝试使用Comma operator

逗号运算符 (,) 计算其每个操作数(从左到 右)并返回最后一个操作数的值。这使您可以创建 对多个表达式求值的复合表达式, 复合表达式的最终值是 其成员表达式的最右边。这通常用于提供 for 循环的多个参数。

【讨论】:

  • 天哪,我的粗心太可怕了,谢谢你的帮助
猜你喜欢
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多