Skip to content
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

setData() can take only full objects #208

Closed
king2 opened this issue Nov 28, 2020 · 4 comments
Closed

setData() can take only full objects #208

king2 opened this issue Nov 28, 2020 · 4 comments

Comments

@king2
Copy link

king2 commented Nov 28, 2020

I have found setData() calls in LightStateWebSocketController.tsx:
const changeLedOn = (event: React.ChangeEvent<HTMLInputElement>) => { setData({ led_on: event.target.checked }, saveData); }

setData() is defined as function that can take 'data' argument of pre-defined type (in WebSocketController.tsx).
If I want to make websockets page that takes many variables from ESP, but each of such variable should be updated separately, I have to make something like this:
const changeLedOn = (event: React.ChangeEvent<HTMLInputElement>) => { //setData({ led_on: event.target.checked }, saveData); var new_data = data; new_data.led_on = event.target.checked; setData(new_data, saveData); }
(because I have another fields in data and must use antire object when updating).
But inside setData() it uses setState() function - and React's setState() can take not full object, just values to be changed.

Is this right?
Maybe setData() should take arguments like React's setState()?

@rjwats
Copy link
Owner

rjwats commented Nov 28, 2020 via email

@king2
Copy link
Author

king2 commented Nov 28, 2020

I tried '...data' and it works, but as I understand, it will translate into object with two values with same key like this:

setData({ counter: 1, led_on: true, led_on: event.target.checked}, saveData); // for example

Is this behavior (take last value from same keys) is defined somewhere, i.e. can I be sure that new value of led_on will be assigned on all browsers, platforms an so on?

I think handeValueChange is best choice in my case as it has determined behavior.

Thanks!

@rjwats
Copy link
Owner

rjwats commented Nov 28, 2020 via email

@king2
Copy link
Author

king2 commented Nov 29, 2020

I will not dig deeper into docs, proposals and trying to find official documents that point to behavior spread operator for same keys in objects, instead of this I will just trust you that last key will override previous one :)

Thank you!

@king2 king2 closed this as completed Nov 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants