8

How to return a boolean value from react native function?

2 Answers 2

5

it can be accomplish like this:

export function isJson(str) {
  try {
    JSON.parse(str);
  } catch (e) {
    return false;
  }
  return true;
}

This function checks whether provided value is valid JSON or not.

Sign up to request clarification or add additional context in comments.

Comments

3

If your function is in a class, you can do this (check toggleBool):

class IsTrue extends Component
{
    state = {isTrue: false}

    componentWillMount = () =>
    {
        const   newVal = this.toggleBool();

        this.setState({isTrue: newVal});
    }

    toggleBool = () =>
    {
        if (this.state.isTrue === false)
            return (true);
        return (false);
    }


    render = () => <Text>{this.state.isTrue}</Text>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.