flow icon indicating copy to clipboard operation
flow copied to clipboard

State cannot be a union in a class derived from ReactComponent

Open pasha-mf opened this issue 11 years ago • 8 comments

type StateA = { mode: "visible"; flipped:  boolean; };
type StateB = { mode: "hidden"; };
type TestCardState = StateA | StateB;

class TestCard extends ReactComponent {
  static defaultProps: {};
  props: {};
  state: TestCardState;

  constructor() {
    super();
    this.state = {mode: "hidden"};
  }

  test() {
    this.setState({mode: "visible", flipped: true});
  }
}

Flow complains about the setState invocation:

test.js:24:5,51: call of method setState
Error:
test.js:13:24,37: prop `flipped` of type parameter `S` of class type: identifier ReactComponent
Property not found in
test.js:10:15,33: object type

test.js:24:5,51: call of method setState
Error:
test.js:24:26,34: string
Expected string literal hidden, got visible instead
test.js:10:23,30: string literal hidden

pasha-mf avatar Sep 22 '15 00:09 pasha-mf

Looks like an issue with the definition of $Shape, which when used with a union is interpreted by Flow to mean "inhabitants can be property-wise assigned to all of the branches of the union" instead of "inhabitants can be property-wise assigned to any of the branches of the union."

So, this breaks:

type U = { x: "A" } | { x: "B" };
declare function f(u: $Shape<U>): void;
f({ x: "A" });

while this works:

type U = { x: "A", y: number } | { x: "B", y: number };
declare function f(u: $Shape<U>): void;
f({ y: 0 });

samwgoldman avatar Sep 22 '15 00:09 samwgoldman

(This also means you should be able to use replaceState, which doesn't require the use of $Shape, to accomplish what you want.)

samwgoldman avatar Sep 22 '15 00:09 samwgoldman

Well, replaceState would have been a nice alternative, except that it gives

Warning: replaceState(...) is deprecated in plain JavaScript React classes. 
Refactor your code to use setState instead (see https://github.com/facebook/react/issues/3236).

and then Uncaught TypeError: this.replaceState is not a function

pasha-mf avatar Sep 22 '15 20:09 pasha-mf

Ah, that's unfortunate. I'm not sure why they deprecated that API. Sorry my knowledge was out of date! We should update the included library files.

I should be able to look into this more in a few days, until then, you might be able to put the object with the union type as the value of a state key instead of using it as the state itself—not what you want to write, I know, but might unblock you.

samwgoldman avatar Sep 22 '15 20:09 samwgoldman

@samwgoldman Thanks for that work around. I've been fighting with this issue for the last two hours and this solves it for now!

In my case though, I'd built a Redux-like store that passed an instance of a class to its listeners:

type Props = {
  stateMachine: StateMachine<Player>
};
export default class PlayerContainer extends Component {
  props: Props;
  state: Player;

  constructor(props: Props) {
    super(props)
    ...

    // when the state changes, the function passed to subscribe is invoked with the new state
    props.stateMachine.subscribe((player: Player) => {
      this.setState({player})
    })
  }
}

Not sure why that caused issues, since there's no union type involved here, but something was clearly messing up $Shape

msiebert avatar May 18 '16 02:05 msiebert

@msiebert From your code snippet, it looks like you intended to write either this.setState(player) or state: { player: Player }. Does changing one or the other remove the type error? If not, can you share the exact error text?

samwgoldman avatar May 18 '16 04:05 samwgoldman

Oops, sorry, I missed that when I was going back to the original state that caused the error. It should have read this.setState(player). Making state: { player: Player } was the workaround that fixed the compile error.

The exact error text was

20: export default class PlayerContainer extends Component {
                                                  ^^^^^^^^^ type parameter `State` of identifier `Component`
 20: export default class PlayerContainer extends Component {
                                                  ^^^^^^^^^ type parameter `State` of identifier `Component`. Property not found in
 22:   state: Player;
              ^^^^^^ Player

msiebert avatar May 18 '16 14:05 msiebert

Since setState() allows you to pass a partial state if you want to use unions for your state type we recommend you nest it 😊

So instead of:

type State = { kind: 'A' } | { kind: 'B' };

You would have:

type Union = { kind: 'A' } | { kind: 'B' };
type State = { union: Union };

This should work great!

calebmer avatar Jul 19 '17 21:07 calebmer