0
votes

In the getEvaluation function I get the number 1, I want to change the state with this value:

type Props = {
};

type State = {
    id_evaluation: string,
};

class Evaluation extends Component < Props, State > {
    state = {
        id_evaluation: '1',
    }

    componentDidMount() {
        const id_eval = getEvaluation();

        this.setState({
            id_evaluation: id_eval,
        });

I checked now 'console.log(typeof(id_eval)), is string.

Flow generates this error:

Cannot call this.setState with object literal bound to partialState because null or undefined [1] is incompatible with string [2].

3
What about the code you have does not work? - Matthew Herbst
I added the Flow error. - Hiago Bonamelli
id_evaluation is string, right? - Dusan Radovanovic
No, I'll convert. But I did the test with the number and not work. - Hiago Bonamelli
@DusanRadovanovic I checked now, it's string yes - Hiago Bonamelli

3 Answers

0
votes

Try to convert id_eval into a string like this.

type Props = {
};

type State = {
    id_evaluation: string,
};

class Evaluation extends Component < Props, State > {
    state = {
        id_evaluation: '1',
    }

    componentDidMount() {
        const id_eval = getEvaluation();

        this.setState({
            id_evaluation: String(id_eval) || '',
        });
0
votes

How about this one:

state = {
  id_evaluation: 1,
};

const id_eval = getEvaluation();
this.setState({
   id_evaluation: id_eval,
});
0
votes

The return value of the getEvaluation() function has a variety of cases, Number type, Null type or Undefined type, but your State limit accepts the String type, which is the error reported in the compilation phase, you need to convert the return value of getEvaluation(), for example using String(id_eval).