Search code examples
reactjseslintprettier

Why am I getting a 'Assignment to property of function parameter ..' eslinterror?


In my reactcomponent I have a prop(array) called events. However I want to reassign values like this:

 events.map(event => {
          event.createdDateTimeConverted = dateFunctions.formatDate(
            event.createdDateTime,
            'hh:mm A, DD MMM YYYY'
          );

          event.assignedDateTime = dateFunctions.formatDate(
            event.assignedDateTime,
            'hh:mm A, DD MMM YYYY'
          );

          event.dueDateTime = dateFunctions.formatDate(
            event.dueDateTime,
            'hh:mm A, DD MMM YYYY'
          );
 }

The code runs fine that is not the problem but I have to refactor because of an eslint/prettier error:

Assignment to property of function parameter 'event'

Why am I getting this error and how can I fix this?


Solution

  • You can add /* eslint-disable no-param-reassign */ at the top of the file.

    OR use

    events.map(ev => {
    
          let event = ev;
    
          event.createdDateTimeConverted = dateFunctions.formatDate(
            event.createdDateTime,
            'hh:mm A, DD MMM YYYY'
          );
    
          event.assignedDateTime = dateFunctions.formatDate(
            event.assignedDateTime,
            'hh:mm A, DD MMM YYYY'
          );
    
          event.dueDateTime = dateFunctions.formatDate(
            event.dueDateTime,
            'hh:mm A, DD MMM YYYY'
          );
    
    }
    

    For more info about this error. you can go to this source