1

I want to return a Boolean value when I run formValidation(), but currently I get undefined. Strangely enough, alert and console.log are working properly.

export function formValidation(...args) {
    let unfilledsObject = [];
    args.map(item => {
        if (this.state[item] === "" || this.state[item] === false)
            unfilledsObject = [...unfilledsObject, item];
    });

     this.setState({ formUnfilleds: unfilledsObject }, () => {
        if (this.state["formUnfilleds"].length) {
            alert(
                `Please fill in all neccessary fields: ${this.state.formUnfilleds}`
            );
            return false;
        }
        if (!this.state["formUnfilleds"].length) {
            console.log("Validated");
            return true;
        }
    });
}
L_Fr
  • 101
  • 1
  • 8
  • 4
    Where's the rest of your function? As is, the obvious answer would be: because your function literally doesn't return anything. There's a function that you've passed to `setState` that returns something, but setState doesn't care about that return value and will completely ignore it. – Mike 'Pomax' Kamermans Jan 20 '20 at 14:51
  • Fixed. Any way I can return either true or false based on how setState resolves? – L_Fr Jan 20 '20 at 14:58
  • You can make your own function an async function, and have it return a promise that you resolve or reject as part of your post-setState function? And then you'll have to remember to call it with `const result = await formValidation(......)` or use the standard Promise `formValidation(.....).then(...).catch(...)` of course. – Mike 'Pomax' Kamermans Jan 20 '20 at 14:59

2 Answers2

1

Try this:

export function formValidation({ ...args,callBack}) {

    let unfilledsObject = [];
    args.map(item => {
        if (this.state[item] === "" || this.state[item] === false)
            unfilledsObject = [...unfilledsObject, item];
    });

     this.setState({ formUnfilleds: unfilledsObject }, () => {
        if (this.state["formUnfilleds"].length) {
            alert(
                `Please fill in all neccessary fields: ${this.state.formUnfilleds}`
            );
            callBack(false);
        }
        if (!this.state["formUnfilleds"].length) {
            console.log("Validated");
            callBack(true);
        }
    });
}

formValidation({...  , boolean => console.log(boolean) }); // ---> you have access to the boolean in callack;

you only return data from a callback you don't return it from formValidation function scope. i use callBack

you can also use Promise. for feather information check this link

Amir Rezvani
  • 1,262
  • 11
  • 34
  • Unfortunately, it only brings console.log("Validated"), the value of function remains undefined... – L_Fr Jan 21 '20 at 08:28
0

Surprisingly enough, this worked:

export async function formValidation(...args) {
    let unfilledsObject = [];
    args.map(item => {
        if (this.state[item] === "" || this.state[item] === false)
            unfilledsObject = [...unfilledsObject, item];
    });

    await this.setState({ formUnfilleds: unfilledsObject }, () => {
        if (this.state["formUnfilleds"].length) {
            alert(
                `Please fill in all neccessary fields: ${this.state.formUnfilleds}`
            );
            return false
        }
        if (!this.state["formUnfilleds"].length) {
            console.log("Validated")
            return true
        }
    });
}```
L_Fr
  • 101
  • 1
  • 8