0

i need to use the NetInfo.isConnected in a variable to send to my if statement, but all examples i see in internet are the same, just console.log the value, never put inside a variable

function verifyConnection() {
  let isConnected;
  NetInfo.fetch().then((state) => {
    isConnected = state.isConnected;
  });
  return isConnected;
}

always return undefined, but if a put into a variable like this

let isConnected = NetInfo.fetch().then((state) => {state.isConnected});

return : LOG {"_A": null, "_x": 0, "_y": 0, "_z": null}

how to use netinfo inside a if statemente to test my connection status

1 Answer 1

1

Use async and await to wait a promise to resolve

async function verifyConnection() {
  return (await NetInfo.fetch()).isConnected;
}

or useNetInfo hook when rendering components.

const Component = () => {
  const netInfo = useNetInfo();

  return (
    <View>
      <Text>Is Connected? {netInfo.isConnected?.toString()}</Text>
    </View>
  );
};
Sign up to request clarification or add additional context in comments.

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.