template-studio icon indicating copy to clipboard operation
template-studio copied to clipboard

setState should follow React best practice

Open DianaLease opened this issue 7 years ago • 6 comments

currently, the pattern used for setting the React component's state is dangerous because const state = this.state does not clone this.state. it's a reference to the state object, which means when you assign state.loading = false you are mutating the state object directly. we could deeply clone the state object and modify the clone, but what we should really do is usesetState to update just the properties on state we want to update.

for example, the current code:

handleLoadingFailed(message) {
        const state = this.state;
        state.loading = false;
        state.loadingFailed = true;
        state.log.loading = message;
        this.setState(state);
    }

would become:

handleLoadingFailed(message) {
        this.setState({
            loading: false,
            loadingFailed: true,
            log: {
                ...this.state.log,
                loading: message
            }
        });
    }

DianaLease avatar Dec 06 '18 23:12 DianaLease

Also, we should avoid multiple consecutive setState calls whenever possible.

jeromesimeon avatar Dec 19 '18 16:12 jeromesimeon

I would like to work on this issue.

b30wulffz avatar Sep 26 '19 01:09 b30wulffz

@b30wulffz I think it may be better to aim for some other repositories we have (like Template Studio v2). We are planning on replacing template-studio soon, so I may have jumped the gun by labeling this issue as Hacktoberfest.

jolanglinais avatar Sep 27 '19 15:09 jolanglinais

Hi, is this issue currently under progress or can it be taken up ?

nik72619c avatar Mar 28 '20 19:03 nik72619c

hi @nik72619c It's not under progress but we aren't pushing more work on template studio at the moment (see news around UX / UI / tooling in Slack from a couple of weeks ago).

There are lots of useful work items on other UI components though! Please check cicero-ui or concerto-ui.

jeromesimeon avatar Mar 28 '20 19:03 jeromesimeon

got it, thanks @jeromesimeon!

nik72619c avatar Mar 28 '20 21:03 nik72619c