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.

State Setters boilerplate: suggestions, counter-arguments?

See original GitHub issue

I find that I am creating a lot of boilerplate setters for each StoreState object.

E.g.

const JobStore = {
    jobData: {},
    jobBoard: [],
    jobList: [],
    setJobData: action((state, payload) => {
        const json = JSON.stringify(payload, ' ', 4)
        state.jobData = json
    }),
    setJobBoard: action((state, payload) => {
        state.jobBoard = payload
    }),
    setJobList: action((state, payload) => {
        state.jobList = payload
    }),

I have to do this because my thunks cannot directly set the state (which I understand the reason for that).

But it is a common feature in OO Programming to be able to automate the creation of property accessors (get/setters) for Object properties.

The userStoreState() makes the getters trivial for store properties. What about some sort of shorthand we can use in a thunk for property setters (a helper function?). I am only talking about items in the same store slice, (eg. setState() and not setStoreState() - but you could have that for folks that want to live dangerously).

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
DrYSGcommented, Sep 2, 2019

One can also make this a bit more robust by using the immer {original} function:

    set: action((state, payload) => {
        const base = Object.keys(original(state))
        Object.entries(payload).forEach(([key, value]) => {
            if (base.includes(key)) 
                state[key] = value
        })
    }),
``
0reactions
ctrlplusbcommented, Oct 28, 2019

@DrYSG - I am going to shy away from introducing additional abstractions as being described above. My gut feeling is that this should belong in user-land. It is fairly easy to introduce your own helpers/abstractions in order to achieve what you feel is necessary on a per case basis.

Thanks for going through this in detail though. Perhaps others can learn from your journey if they visit this issue. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

programming languages - A defense for boilerplate?
What may look like boring, repetitive boilerplate to you as you're writing ... all languages are as wasteful as the getters and setters...
Read more >
"I still stubbornly believe the whole “private members accessed via ...
Version 1: I pass the sprite object to the renderer, and the renderer gets the texture contained in the sprite and draws it,...
Read more >
How to avoid having to write boilerplate accessor code
So, my strategy for avoiding boilerplate code: Try to avoid getters and setters. When in need of some pure data class I declare...
Read more >
Strategic Scala Style: Principle of Least Power
"Setter Injection" refers to instantiating an object, and then setting some variable onto that object which it will use when you call methods...
Read more >
The Language I Wish Go Was - Bob Nystrom - stuffwithstuff
I hate the boilerplate of Java and the compile times of C++. ... You have no idea what state it's in. ... Property...
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