question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incoming arg to updater cannot be modified

See original GitHub issue

When using the update function from ShiftDB, the prevState argument appears to be ‘read-only’. I’m unaware of any spec which defines such functionality, so as of now I assume this is unintended.

update((someData = { key: [] }) => {
  someData.key.push('item');
  return someData;
});

The above code results in the following error

TypeError: Cannot assign to read only property 'key' of #<Object>

I suggest initially and immediately reverting this to the specced behavior, and then we can open a discussion regarding future design.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
arikmaorcommented, Sep 2, 2019

I don’t mind removing the actual freezing, (also, I think it’s a must in production) For all other means, this should be considered immutable to allow us to handle the data changes for the user more efficiently. I would also like to stay consistent with React.setState

React Documentation

from: https://reactjs.org/docs/state-and-lifecycle.html#using-state-correctly

Do Not Modify State Directly For example, this will not re-render a component:

// Wrong this.state.comment = ‘Hello’; Instead, use setState():

// Correct this.setState({comment: ‘Hello’}); The only place where you can assign this.state is the constructor.

So in react it’s not allowed and frowned upon mutating the state. The whole react mechanism works on the assumption that everything is immutable.

React with TypeScript

I wrote this code:

import React from 'react';

interface MyComponentProps {
}

interface MyComponentState {
  something: string;
}

export default class MyComponent extends React.Component<MyComponentProps, MyComponentState> {

  state = {
    something: 'bla',
  }

  doSomething = () => {
    this.state.something = 'wrong'; // No TypeScript warning here, though it's documented not to change this
    this.setState((state) => {
      state.something = 'else'; // (property) [something: string] Cannot assign to 'something' because it is a read-only property.ts(2540)
      return state;
    });
  }

  render() {
    return <div>{this.state.something}<button onClick={this.doSomething}>bla</button></div>
  }
}

So leaving this object read only is perfectly consistent with React

0reactions
ashevatcommented, Sep 24, 2019

LGTM, pending our launch and user feedback.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is modifying an incoming parameter an antipattern? [closed]
It is an error to modify an in parameter or read an out parameter, but any changes to an in out parameter are...
Read more >
Java: Modifying parameter passed as a Reference
I have to update the value of result parameter before method returns. For example if the value of result was ABC when the...
Read more >
Remedy AR System Server- Email - Approval e-mail is getting ...
Remedy AR System Server- Email - Approval e-mail is getting rejected showing "The modify key in the incoming modify action email message is...
Read more >
Update your stack set - AWS CloudFormation
In this walkthrough, we are using the current template. Choose Use current template, and then choose Next. On the Specify StackSet details page,...
Read more >
Windows Update error codes by component - Microsoft Learn
Learn about reference information for Windows Update error codes, including automatic update errors, UI errors, and reporter errors.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found