简体   繁体   English

相邻的 JSX 元素必须包含在 react native 中

[英]Adjacent JSX elements must be wrapped in an enclosing in react native

I'm trying to add an action buttons to my shedule app我正在尝试向我的shedule应用程序添加一个操作按钮

source code源代码

import React, { Component } from 'react';
import {
  Text,
  View,
  StyleSheet
} from 'react-native';
import {Agenda} from 'react-native-calendars';

import ActionButton from 'react-native-action-button';
import Icon from 'react-native-vector-icons/Ionicons';

export default class AgendaScreen extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: {}
    };
  }

  render() {
    return (
      <Agenda
        items={this.state.items}
        loadItemsForMonth={this.loadItems.bind(this)}
        selected={'2017-05-16'}
        renderItem={this.renderItem.bind(this)}
        renderEmptyDate={this.renderEmptyDate.bind(this)}
        rowHasChanged={this.rowHasChanged.bind(this)}

      />
      <View style={{flex:1, backgroundColor: '#f3f3f3'}}>
        {/* Rest of the app comes ABOVE the action button component !*/}
        <ActionButton buttonColor="rgba(231,76,60,1)">
          <ActionButton.Item buttonColor='#9b59b6' title="Create Shedule" onPress={() => console.log("Create Shedule!")}>
            <Icon name="md-create" style={styles.actionButtonIcon} />
          </ActionButton.Item>
          <ActionButton.Item buttonColor='#3498db' title="Notifications" onPress={() => {}}>
            <Icon name="md-notifications-off" style={styles.actionButtonIcon} />
          </ActionButton.Item>
          <ActionButton.Item buttonColor='#1abc9c' title="All Tasks" onPress={() => {}}>
            <Icon name="md-done-all" style={styles.actionButtonIcon} />
          </ActionButton.Item>
        </ActionButton>
      </View>
    );
  this.confirmDate = this.confirmDate.bind(this);
  this.openCalendar = this.openCalendar.bind(this);
  }

  loadItems(day) {
    setTimeout(() => {
      for (let i = -15; i < 85; i++) {
        const time = day.timestamp + i * 24 * 60 * 60 * 1000;
        const strTime = this.timeToString(time);
        if (!this.state.items[strTime]) {
          this.state.items[strTime] = [];
          const numItems = Math.floor(Math.random() * 5);
          for (let j = 0; j < numItems; j++) {
            this.state.items[strTime].push({
              name: 'Item for ' + strTime,
              height: Math.max(50, Math.floor(Math.random() * 150))
            });
          }
        }
      }
      //console.log(this.state.items);
      const newItems = {};
      Object.keys(this.state.items).forEach(key => {newItems[key] = this.state.items[key];});
      this.setState({
        items: newItems
      });
    }, 1000);
    // console.log(`Load Items for ${day.year}-${day.month}`);
  }

  renderItem(item) {
    return (
      <View style={[styles.item, {height: item.height}]}><Text>{item.name}</Text></View>
    );
  }

  renderEmptyDate() {
    return (
      <View style={styles.emptyDate}><Text>This is empty date!</Text></View>
    );
  }

  rowHasChanged(r1, r2) {
    return r1.name !== r2.name;
  }

  timeToString(time) {
    const date = new Date(time);
    return date.toISOString().split('T')[0];
  }
}

const styles = StyleSheet.create({
  item: {
    backgroundColor: 'white',
    flex: 1,
    borderRadius: 5,
    padding: 10,
    marginRight: 10,
    marginTop: 17
  },
  emptyDate: {
    height: 15,
    flex:1,
    paddingTop: 30
  }
}

actionButtonIcon: {
    fontSize: 20,
    height: 22,
    color: 'white',
  },);

This is my action button source code ( it's also included in the above code )这是我的动作按钮源代码它也包含在上面的代码中

import React, { Component } from 'react';

import ActionButton from 'react-native-action-button';
import Icon from 'react-native-vector-icons/Ionicons';
import { StyleSheet, Text, View } from 'react-native';





export default function App() {
  return (
   <View style={{flex:1, backgroundColor: '#f3f3f3'}}>
        {/* Rest of the app comes ABOVE the action button component !*/}
        <ActionButton buttonColor="rgba(231,76,60,1)">
          <ActionButton.Item buttonColor='#9b59b6' title="Create Shedule" onPress={() => console.log("Create Shedule!")}>
            <Icon name="md-create" style={styles.actionButtonIcon} />
          </ActionButton.Item>
          <ActionButton.Item buttonColor='#3498db' title="Notifications" onPress={() => {}}>
            <Icon name="md-notifications-off" style={styles.actionButtonIcon} />
          </ActionButton.Item>
          <ActionButton.Item buttonColor='#1abc9c' title="All Tasks" onPress={() => {}}>
            <Icon name="md-done-all" style={styles.actionButtonIcon} />
          </ActionButton.Item>
        </ActionButton>
      </View>
    );
  }



const styles = StyleSheet.create({
  actionButtonIcon: {
    fontSize: 20,
    height: 22,
    color: 'white',
  },
});

This is the error i'm getting这是我得到的错误我得到的错误

This is how it should look like这应该是这样的看法

from here you can get the calendar & this is the action button I tried to add the action buttons but I keep getting the above error Can you help me to fix this issue这里你可以得到日历&这是操作按钮,我试图添加操作按钮,但我不断收到上述错误你能不能帮我解决这个问题

# #

You can try to wrap Agenda and View components in a fragment tag.您可以尝试将 Agenda 和 View 组件包装在片段标签中。

<>
    <Agenda />
    <View />
</>

It should work它应该工作

The empty tag is a shortcut for <React.Fragment>空标签是<React.Fragment>的快捷方式

According to reacts jsx architecture, everything must be wrapped inside a parent jsx element.根据 reacts jsx 架构,所有内容都必须包含在父 jsx 元素中。 So in your case and doesn't have a parent tag, so either wrap it around a or a <>.所以在你的情况下并且没有父标签,所以要么将它包裹在 a 或 <> 周围。 Like the below像下面这样

<> <>

Or或者

Hope it helps.希望能帮助到你。 Feel free for doubts.如有疑问,请放心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 相邻的JSX元素必须包装在一个封闭的标记React Native中 - Adjacent JSX elements must be wrapped in an enclosing tag React Native 如何修复错误:相邻的 JSX 元素必须包含在封闭标记中? 在本机反应 - How to fix the error: Adjacent JSX elements must be wrapped in an enclosing tag ? in react native 相邻的 JSX 元素必须包含在图像的封闭标签中吗? - Adjacent JSX elements must be wrapped in an enclosing tag for Images? 错误:相邻的JSX元素必须包装在封闭标记中 - Error: Adjacent JSX elements must be wrapped in an enclosing tag 解析错误:必须将相邻的 JSX 元素包装在封闭标记中 - Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag 相邻的JSX元素必须包装在一个封闭标签中-gatsby.js - Adjacent JSX elements must be wrapped in an enclosing tag - gatsby.js 必须将相邻的JSX元素包装在带有换行符标记的封闭标记中 - Adjacent JSX elements must be wrapped in an enclosing tag with line break tag 解析错误:相邻的 JSX 元素必须包含在封闭标记中 - Parse Error: Adjacent JSX elements must be wrapped in an enclosing tag 预期的“}”或相邻的 JSX 元素必须包含在封闭标记中 - Expected "}" OR Adjacent JSX elements must be wrapped in an enclosing tag Reactstrap:“相邻的 JSX 元素必须包裹在封闭标签中”? - Reactstrap: "Adjacent JSX elements must be wrapped in an enclosing tag"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM