SkyeBoniwell
SkyeBoniwell

Reputation: 7122

'arguments' is only allowed in functions and class methods React

I have a simple React component that updates a few things.

I am getting this error when I try to run it:

'arguments' is only allowed in functions and class methods.

The error is in thos component, in the updateConnection function:

import { updateConnection } from '../connections';

export default class Connection extends React.PureComponent {

    updateConnection = (name, value, save = true) => {
        if (arguments.length === 0) {
            updateConnection(this.state.connection);
            return;
        }
        

The updateConnection that is imported, is just a simple API call:

export function updateConnection(connection) {
    return api.put(`/api/connections/${connection.id}`, connection);
}

But updateConnection is a function...so why would it give me that error?

Full error:

SyntaxError: G:\ConnectMe\Connect\Client\Connector\components\Connection.js: 'arguments' is only allowed in functions and class methods. (124:12)

Upvotes: 0

Views: 252

Answers (1)

Haseeb Anwar
Haseeb Anwar

Reputation: 2918

It looks like you are trying to use the arguments keyword in an arrow function. The keyword arguments cannot be used in arrow functions.

const myFunction = () => {
  // cannot use arguments in an arrow function
  console.log(arguments);
}

to use arguments in an arrow function, you can use the spread operator

const myFunction = (...args) => {
  console.log(args); // args contains function's arguments as an Array
}

Though, in function declarations, you can use the arguments keyword.

function myFuction () {
 console.log(arguments); // can be used here
}

Or, you can use it in class methods

class MyClass {
  classMethod () {
    console.log(arguments); // can be used here
  }
}

Upvotes: 2

Related Questions