Parsing error jsx attributes must only be assigned a non empty expression

I am trying to create a custom drawer in react-navigation. However I am getting an error. The error is JSX attributes must only be assigned a non-empty expression. I have even create the array and ...

I am trying to create a custom drawer in react-navigation. However I am getting an error. The error is JSX attributes must only be assigned a non-empty expression. I have even create the array and maped it to show but still getting an error. Did i miss anything?

import { StackNavigator, DrawerNavigator } from 'react-navigation';
import { View, Text, Image, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';        

const navitems =[
    {
      name:'Home',
      nav:'classesnew',
    },
    {
      name:'Device',
      nav:'start',
    },
]


class DrawerContent extends React.Component{
  constructor(props) {
    super(props)
  }
  render(){
    return  (
      <Image source={require('../images/logo.png')}
      style={styles.container}>
              <View style={{justifyContent: 'center',
              alignItems: 'center',}}>
                <Image style={styles.image} source={{uri: ''}}/>
              </View>
              <View>
              {
                  navitems.map((l,i)=>{
                    return (
                      <TouchableOpacity
                          key={i}
                          style={{marginBottom:0.5}}
                          onPress={()=>{
                                        this.props.navigation.navigate(l.nav)
                                        }
                      }>
                        <View style={{flexDirection:'row', padding: 15, paddingLeft:0, backgroundColor:'#fff0', borderTopWidth:0.5, borderColor:'rgba(255,255,255, 0.5)', marginLeft: 20, marginRight:20}}>
                          <Icon name={l.icon} size={20} style={{paddingLeft:10, paddingRight: 20, height: 25,  }} color="#ffffff" />
                          <Text style={{fontSize:16, fontWeight: 'bold',color:'#fff'}}>{l.name}</Text>
                        </View>
                      </TouchableOpacity>)
                  })
              }
              </View>
          </Image>)
  }
}

const DrawerRoutes = (
  {
      Main: { screen: App, title: 'Main' },
      Device: { screen: Device, title: 'Device' },
  })

const Drawer = DrawerNavigator(DrawerRoutes ,{
  contentComponent:({navigation})=> <DrawerContent navigation={navigation} routes={DrawerRoutes} />,
});


Drawer.navigationOptions = {

  contentOptions: {
    activeBackgroundColor: '#ff5976',
    style: {
      backgroundColor: '#000000',
      zIndex: 100,
      paddingTop: 0
    }
  },
  header: ({ state, setParams, navigate, dispatch })  => ({
    visible: true,
    tintColor: '#ffffff',
    title: "LokaLocal",
    style: {
      backgroundColor: '#ff5976'
    },
    right: (
        <TouchableOpacity
          onPress={() => navigate('DrawerOpen')}
        >
          <Icon name="search" size={16} style={{ padding: 10, paddingRight: 20 }} color="#ffffff" />
        </TouchableOpacity>
      ),
    left: (
        <TouchableOpacity
          onPress={}
        >
          <Icon name="bars" size={16} style={{ padding: 10, paddingLeft: 20 }} color="#ffffff" />
        </TouchableOpacity>
      ),
  })
}


export const Routes = StackNavigator(
  {
    Login: { screen: Login },
    Dashboard: {screen: Drawer},
  },
);

I am using react-navigation 1.0.0.bet.9

I am trying to create a custom drawer in react-navigation. However I am getting an error. The error is JSX attributes must only be assigned a non-empty expression. I have even create the array and maped it to show but still getting an error. Did i miss anything?

import { StackNavigator, DrawerNavigator } from 'react-navigation';
import { View, Text, Image, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';        

const navitems =[
    {
      name:'Home',
      nav:'classesnew',
    },
    {
      name:'Device',
      nav:'start',
    },
]


class DrawerContent extends React.Component{
  constructor(props) {
    super(props)
  }
  render(){
    return  (
      <Image source={require('../images/logo.png')}
      style={styles.container}>
              <View style={{justifyContent: 'center',
              alignItems: 'center',}}>
                <Image style={styles.image} source={{uri: ''}}/>
              </View>
              <View>
              {
                  navitems.map((l,i)=>{
                    return (
                      <TouchableOpacity
                          key={i}
                          style={{marginBottom:0.5}}
                          onPress={()=>{
                                        this.props.navigation.navigate(l.nav)
                                        }
                      }>
                        <View style={{flexDirection:'row', padding: 15, paddingLeft:0, backgroundColor:'#fff0', borderTopWidth:0.5, borderColor:'rgba(255,255,255, 0.5)', marginLeft: 20, marginRight:20}}>
                          <Icon name={l.icon} size={20} style={{paddingLeft:10, paddingRight: 20, height: 25,  }} color="#ffffff" />
                          <Text style={{fontSize:16, fontWeight: 'bold',color:'#fff'}}>{l.name}</Text>
                        </View>
                      </TouchableOpacity>)
                  })
              }
              </View>
          </Image>)
  }
}

const DrawerRoutes = (
  {
      Main: { screen: App, title: 'Main' },
      Device: { screen: Device, title: 'Device' },
  })

const Drawer = DrawerNavigator(DrawerRoutes ,{
  contentComponent:({navigation})=> <DrawerContent navigation={navigation} routes={DrawerRoutes} />,
});


Drawer.navigationOptions = {

  contentOptions: {
    activeBackgroundColor: '#ff5976',
    style: {
      backgroundColor: '#000000',
      zIndex: 100,
      paddingTop: 0
    }
  },
  header: ({ state, setParams, navigate, dispatch })  => ({
    visible: true,
    tintColor: '#ffffff',
    title: "LokaLocal",
    style: {
      backgroundColor: '#ff5976'
    },
    right: (
        <TouchableOpacity
          onPress={() => navigate('DrawerOpen')}
        >
          <Icon name="search" size={16} style={{ padding: 10, paddingRight: 20 }} color="#ffffff" />
        </TouchableOpacity>
      ),
    left: (
        <TouchableOpacity
          onPress={}
        >
          <Icon name="bars" size={16} style={{ padding: 10, paddingLeft: 20 }} color="#ffffff" />
        </TouchableOpacity>
      ),
  })
}


export const Routes = StackNavigator(
  {
    Login: { screen: Login },
    Dashboard: {screen: Drawer},
  },
);

I am using react-navigation 1.0.0.bet.9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.

Already on GitHub?
Sign in
to your account


Closed

iHadDream opened this issue

Sep 17, 2018

· 5 comments

Comments

@iHadDream

Do you want to request a feature or report a bug?
Honestly I don’t know

What is the current behavior?
why JSX attributes must only be assigned a non-empty expression ?

@TryingToImprove

The reason is that JSX is transpiled into a JS function meaning that;

<MyComponent name="React" />

gets converted into

React.createElement(MyComponent, { name: 'React' })

If a empty expression was made as name={} it would be converted into

React.createElement(MyComponent, { name: })

which will result in invalid JavaScript..

See: https://reactjs.org/docs/jsx-in-depth.html for more info

@rohitkrops

@TryingToImprove is right, you must assign a non-empty expression or don’t include it. Valid definitions can be

// props passed down
<MyComponent name="React" />

// without props 
<MyComponent />

If you are using the first expression better use Proptypes with default props.

Cheers

@nhunzaker

Thanks everyone for helping out!

@iHadDream I’m going to close this out because we like to keep this issue board focused on bugs/improvements to React itself, however please checkout our support page for discussion boards and other sources to get help:

https://reactjs.org/community/support.html

Have a great day!

@iHadDream

The reason is that JSX is transpiled into a JS function meaning that;

<MyComponent name="React" />

gets converted into

React.createElement(MyComponent, { name: 'React' })

If a empty expression was made as name={} it would be converted into

React.createElement(MyComponent, { name: })

which will result in invalid JavaScript..

See: https://reactjs.org/docs/jsx-in-depth.html for more info

thank you ~

@trustidkid

Line 14: Parsing error: JSX attributes must only be assigned a non-empty expression

12 |
13 |

14 |
| ^
15 |
16 |
17 |
What is the write keywords to use here?

This article will show a few solutions to fix JSX attributes that must only be assigned a non-empty expression, such as adding a value to the attribute or remove the attribute.

The reason for the error JSX attributes must only be assigned a non-empty expression

This error occurs when the program realizes that you are leaving one or more attributes of elements blank in the JSX code. On the console screen, an error will appear JSX attributes must only be assigned a non-empty expression so the user can know and debug the code.

Code:

import React from 'react';

const App = () => {

  return (
    <div>
     <button onClick={}>Click me!!</button>
    </div>
  );
};

export default App;

Output:

For example, in the above code, the button’s onClick attribute is empty, so Eslint will recognize this and report an error.

How to fix this error

Adding a value to the attribute 

We will add a value to the element’s empty attribute to fix this. Eslint will recognize the attribute has been set to a valid value, and the JSX attributes must only be assigned a non-empty expression error no longer appears. Follow the code below to learn how to fix the error with this method.

Code:

import React from 'react';

const handleClick=()=>{
console.log("You have clicked the button");
}

const App = () => {
  return (
    <div>
     <button onClick={handleClick}>Click me!!</button>
    </div>
  );
};

export default App;

Output:

When you pass a function value to the button element’s onClick attribute, the program knows that the attribute is no longer empty. The Eslint warning error also disappeared, and the program ran usually. Or you can refer to the solution below.

Remove the attribute

If you don’t know what to fill in the attribute or need to show it for debugging, you can also remove it so that the error no longer appears. 

Code:

import React from 'react';

const App = () => {

  return (
    <div>
     <button>Click me!!</button>
    </div>
  );
};

export default App;

This is a workaround for React programmers to prevent the error from appearing to continue with the code, or when you find the value to set for the property, you can substitute it for the attribute value. Good luck with these solutions

Summary

To summarize, the article has shown you two solutions to prevent the JSX attributes must only be assigned a non-empty expression error from appearing during the coding process. You should use the solution of adding a value to the attribute because it will be a permanent solution.

Nathaniel Kirk

Hi, my name’s Nathaniel Kirk. I’m interested in learning and sharing knowledge about programming languages. My strengths are C, C++, Python, HTML, CSS, Javascript, and Reactjs. If you have difficulty with them, don’t worry. I’m here to support you.


Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Python, HTML, CSS, Javascript, Reactjs

I am trying to create a custom drawer in react-navigation. However I am getting an error. The error is JSX attributes must only be assigned a non-empty expression. I have even create the array and maped it to show but still getting an error. Did i miss anything?

import { StackNavigator, DrawerNavigator } from 'react-navigation';
import { View, Text, Image, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';        

const navitems =[
    {
      name:'Home',
      nav:'classesnew',
    },
    {
      name:'Device',
      nav:'start',
    },
]


class DrawerContent extends React.Component{
  constructor(props) {
    super(props)
  }
  render(){
    return  (
      <Image source={require('../images/logo.png')}
      style={styles.container}>
              <View style={{justifyContent: 'center',
              alignItems: 'center',}}>
                <Image style={styles.image} source={{uri: ''}}/>
              </View>
              <View>
              {
                  navitems.map((l,i)=>{
                    return (
                      <TouchableOpacity
                          key={i}
                          style={{marginBottom:0.5}}
                          onPress={()=>{
                                        this.props.navigation.navigate(l.nav)
                                        }
                      }>
                        <View style={{flexDirection:'row', padding: 15, paddingLeft:0, backgroundColor:'#fff0', borderTopWidth:0.5, borderColor:'rgba(255,255,255, 0.5)', marginLeft: 20, marginRight:20}}>
                          <Icon name={l.icon} size={20} style={{paddingLeft:10, paddingRight: 20, height: 25,  }} color="#ffffff" />
                          <Text style={{fontSize:16, fontWeight: 'bold',color:'#fff'}}>{l.name}</Text>
                        </View>
                      </TouchableOpacity>)
                  })
              }
              </View>
          </Image>)
  }
}

const DrawerRoutes = (
  {
      Main: { screen: App, title: 'Main' },
      Device: { screen: Device, title: 'Device' },
  })

const Drawer = DrawerNavigator(DrawerRoutes ,{
  contentComponent:({navigation})=> <DrawerContent navigation={navigation} routes={DrawerRoutes} />,
});


Drawer.navigationOptions = {

  contentOptions: {
    activeBackgroundColor: '#ff5976',
    style: {
      backgroundColor: '#000000',
      zIndex: 100,
      paddingTop: 0
    }
  },
  header: ({ state, setParams, navigate, dispatch })  => ({
    visible: true,
    tintColor: '#ffffff',
    title: "LokaLocal",
    style: {
      backgroundColor: '#ff5976'
    },
    right: (
        <TouchableOpacity
          onPress={() => navigate('DrawerOpen')}
        >
          <Icon name="search" size={16} style={{ padding: 10, paddingRight: 20 }} color="#ffffff" />
        </TouchableOpacity>
      ),
    left: (
        <TouchableOpacity
          onPress={}
        >
          <Icon name="bars" size={16} style={{ padding: 10, paddingLeft: 20 }} color="#ffffff" />
        </TouchableOpacity>
      ),
  })
}


export const Routes = StackNavigator(
  {
    Login: { screen: Login },
    Dashboard: {screen: Drawer},
  },
);

I am using react-navigation 1.0.0.bet.9

Я пытаюсь создать пользовательский ящик в интерактивной навигации. Однако я получаю сообщение об ошибке. Ошибка в JSX attributes must only be assigned a non-empty expression. Я даже создал массив и отобразил его, чтобы показать, но все еще получаю сообщение об ошибке. Я что-то пропустил?

import { StackNavigator, DrawerNavigator } from 'react-navigation';
import { View, Text, Image, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';        

const navitems =[
    {
      name:'Home',
      nav:'classesnew',
    },
    {
      name:'Device',
      nav:'start',
    },
]


class DrawerContent extends React.Component{
  constructor(props) {
    super(props)
  }
  render(){
    return  (
      <Image source={require('../images/logo.png')}
      style={styles.container}>
              <View style={{justifyContent: 'center',
              alignItems: 'center',}}>
                <Image style={styles.image} source={{uri: ''}}/>
              </View>
              <View>
              {
                  navitems.map((l,i)=>{
                    return (
                      <TouchableOpacity
                          key={i}
                          style={{marginBottom:0.5}}
                          onPress={()=>{
                                        this.props.navigation.navigate(l.nav)
                                        }
                      }>
                        <View style={{flexDirection:'row', padding: 15, paddingLeft:0, backgroundColor:'#fff0', borderTopWidth:0.5, borderColor:'rgba(255,255,255, 0.5)', marginLeft: 20, marginRight:20}}>
                          <Icon name={l.icon} size={20} style={{paddingLeft:10, paddingRight: 20, height: 25,  }} color="#ffffff" />
                          <Text style={{fontSize:16, fontWeight: 'bold',color:'#fff'}}>{l.name}</Text>
                        </View>
                      </TouchableOpacity>)
                  })
              }
              </View>
          </Image>)
  }
}

const DrawerRoutes = (
  {
      Main: { screen: App, title: 'Main' },
      Device: { screen: Device, title: 'Device' },
  })

const Drawer = DrawerNavigator(DrawerRoutes ,{
  contentComponent:({navigation})=> <DrawerContent navigation={navigation} routes={DrawerRoutes} />,
});


Drawer.navigationOptions = {

  contentOptions: {
    activeBackgroundColor: '#ff5976',
    style: {
      backgroundColor: '#000000',
      zIndex: 100,
      paddingTop: 0
    }
  },
  header: ({ state, setParams, navigate, dispatch })  => ({
    visible: true,
    tintColor: '#ffffff',
    title: "LokaLocal",
    style: {
      backgroundColor: '#ff5976'
    },
    right: (
        <TouchableOpacity
          onPress={() => navigate('DrawerOpen')}
        >
          <Icon name="search" size={16} style={{ padding: 10, paddingRight: 20 }} color="#ffffff" />
        </TouchableOpacity>
      ),
    left: (
        <TouchableOpacity
          onPress={}
        >
          <Icon name="bars" size={16} style={{ padding: 10, paddingLeft: 20 }} color="#ffffff" />
        </TouchableOpacity>
      ),
  })
}


export const Routes = StackNavigator(
  {
    Login: { screen: Login },
    Dashboard: {screen: Drawer},
  },
);

Я использую response-navigation 1.0.0.bet.9


0

Do you want to request a feature or report a bug?
Honestly I don’t know

What is the current behavior?
why JSX attributes must only be assigned a non-empty expression ?

iHadDream picture
iHadDream
 · 
17 Sep 2018

All comments


4

The reason is that JSX is transpiled into a JS function meaning that;

<MyComponent name="React" />

gets converted into

React.createElement(MyComponent, { name: 'React' })

If a empty expression was made as name={} it would be converted into

React.createElement(MyComponent, { name: })

which will result in invalid JavaScript..

See: https://reactjs.org/docs/jsx-in-depth.html for more info

TryingToImprove picture
TryingToImprove
 · 
17 Sep 2018


2

@TryingToImprove is right, you must assign a non-empty expression or don’t include it. Valid definitions can be

// props passed down
<MyComponent name="React" />

// without props 
<MyComponent />

If you are using the first expression better use Proptypes with default props.

Cheers

Rohitkrops picture
Rohitkrops
 · 
17 Sep 2018


1

Thanks everyone for helping out!

@iHadDream I’m going to close this out because we like to keep this issue board focused on bugs/improvements to React itself, however please checkout our support page for discussion boards and other sources to get help:

https://reactjs.org/community/support.html

Have a great day!

nhunzaker picture
nhunzaker
 · 
18 Sep 2018


0

The reason is that JSX is transpiled into a JS function meaning that;

<MyComponent name="React" />

gets converted into

React.createElement(MyComponent, { name: 'React' })

If a empty expression was made as name={} it would be converted into

React.createElement(MyComponent, { name: })

which will result in invalid JavaScript..

See: https://reactjs.org/docs/jsx-in-depth.html for more info

thank you ~

iHadDream picture
iHadDream
 · 
18 Sep 2018


0

Line 14: Parsing error: JSX attributes must only be assigned a non-empty expression

12 |
13 |

14 |
| ^
15 |
16 |
17 |

What is the write keywords to use here?

trustidkid picture
trustidkid
 · 
16 Apr 2019

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Parsing error firebird
  • Partition read error перевод
  • Partition read error testdisk что делать
  • Partition preloader no image file exist как исправить
  • Parsing error file is not allowed as an identifier people playground

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии