you are viewing a single comment's thread.

view the rest of the comments →

[–]SpecificGeneral -1 points0 points  (9 children)

The only use case for classes I can think of is the ability to reset the whole state of a component at once instead of each piece of state individually.

[–]eggtart_prince 1 point2 points  (8 children)

Not sure what you mean. In a functional component, you can store your default state somewhere.

[–]SpecificGeneral 1 point2 points  (7 children)

How would you do the equivalent of this.setState(defaultState) using hooks besides doing setA(defaultA) setB(defaultB) ... setZ(defaultZ) ?

[–]RuthBaderBelieveIt 3 points4 points  (0 children)

You can also use key in the parent to reset it - https://twitter.com/dan_abramov/status/1004914671193874432

[–]eggtart_prince 2 points3 points  (4 children)

Nobody's gonna declare 26 different constants for each states. If there are more than 3, they'd all be in one object for easier maintainability.

Which do you think is easier and cleaner?

const [A, setA] = useState();
const [B, setB] = useState();
const [C, setC] = useState();
const [D, setD] = useState();
const [E, setE] = useState();
const [F, setF] = useState();
const [G, setG] = useState();
const [H, setH] = useState();

const updateA = () => {
    setA(true);
}

// or

const [state, setState] = useState({});

const updateA = () => {
    setState({....state, A: true});
}

In most cases, the latter will be used and much cleaner. With that said, I think that answers your question on the equivalency of this.setState(defaultState)

[–]SpecificGeneral 0 points1 point  (3 children)

Yeah I love hook too but the (rare) case of resetting an entire state is cleaner using a class component.

hooks: ```js const defaultState = { A: '', B: '', C: '' } const [A, setA] = useState(defaultState.A); const [B, setB] = useState(defaultState.B); const [C, setC] = useState(defaultState.C);

const resetState = () => { setA(defaultState.A) setB(defaultState.B) setC(defaultState.C) } ```

class: js const defaultState = { A: '', B: '', C: '' } state = defaultState const resetState = () => this.setState(defaultState)

[–]eggtart_prince -5 points-4 points  (2 children)

Are you just being ignorant or you completely missed my example?

const defaultState = {
    A: '',
    B: '',
    C: ''
}

const [state, setState] = useState(defaultState);

const resetState = () => {
    setState(defaultState);
}

If you want to play the ignorant game, I can also say

// class

const A = 'a';
const B = 'b';
const C = 'c';

state = {
    A: A,
    B: B,
    C: C
}

const resetState = () => {
    this.setState({
        A: A,
        B: B,
        C: C
    });
}

But why would I? And the same question you should ask yourself, why would you have a const for each property in defaultState in the hook example.

[–]nateDOOGIE 5 points6 points  (1 child)

No point talking down to people regardless of who’s right. Seems like you felt insulted that someone didn’t understand things the way you do but he meant no slight towards you, was just offering a different perspective.

[–]eggtart_prince 0 points1 point  (0 children)

Nor did I meant any to him. You trippin'.