【问题标题】:React Native declaring arrart inside component keeps throwing Syntax ErrorReact Native在组件内声明arrart不断抛出语法错误
【发布时间】:2023-04-09 13:48:01
【问题描述】:

我不断收到来自 React Native 的 SyntaxError 抱怨我的数组声明,我没有看到迫在眉睫的问题,我错过了什么?我知道这可能是我想念但看不到的非常简单的东西。

这是我的组件:

class More extends Component {

  const items = [
    {
      title: 'Job Board',
      icon: 'home',
      screen: 'JobBoard'
    },
    {
      title: 'Leaderboard',
      icon: 'home',
      screen: 'Leaderboard'
    },
    {
      title: 'Resources',
      icon: 'home',
      screen: 'Resources'
    },
    {
      title: 'Check In',
      icon: 'home',
      screen: 'CheckIn'
    },
    {
      title: 'About',
      icon: 'home',
      screen: 'About'
    }
  ];

  render() {
    const { navigate } = this.props.navigation;

    return (
      <View style = {styles.container}>
        <List>
          {
            items.map((item, i) => {
              <ListItem
                key={i}
                title={item.title}
                leftIcon={{name: item.icon}}
                onPress={() => navigate(item.screen)}
              />
            });
          }
        </List>
      </View>
    );
  };
}

const styles = StyleSheet.create({});


export { More };

Error from simulator Packager feedback

【问题讨论】:

    标签: react-native syntax-error


    【解决方案1】:

    您有 2 个选项,要么在 class 之外定义 const items = [...],要么在 constructor 中定义它并将其设置为 state

      constructor(){
        super();
        const items = [
          {
            title: 'Job Board',
            icon: 'home',
            screen: 'JobBoard'
          },
          {
            title: 'Leaderboard',
            icon: 'home',
            screen: 'Leaderboard'
          },
          {
            title: 'Resources',
            icon: 'home',
            screen: 'Resources'
          },
          {
            title: 'Check In',
            icon: 'home',
            screen: 'CheckIn'
          },
          {
            title: 'About',
            icon: 'home',
            screen: 'About'
          }
        ];
        this.state = {
          items
        }
      }
    
    
    render() {
        const { navigate } = this.props.navigation;
    
        return (
            <View style = {styles.container}>
                <List>
                    {
                        this.state.items.map((item, i) => {
                            <ListItem
                                key={i}
                                title={item.title}
                                leftIcon={{name: item.icon}}
                                onPress={() => navigate(item.screen)}
                            />
                        });
                    }
                </List>
            </View>
        );
    };
    

    【讨论】:

    • 谢谢。尽管我之前曾尝试过,但由于某种原因它不起作用,但我还是带着新的想法进来的,现在它确实起作用了。固定的。我正在使用 Redux,所以现在只是在类之外定义它。再次感谢您!
    • 不客气。听起来不错,因为问题解决了!如果您发现我的解决方案有用,请标记为有用,以便其他用户也会发现它有帮助。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2020-09-20
    • 1970-01-01
    • 2022-09-22
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    相关资源
    最近更新 更多