0

i want to use service in react-native using native modules ,if Wifi connectivity change from off to on just show toast that connection change

Majid Aziz
  • 64
  • 10

1 Answers1

0

You can use react-native-netinfo plugin. Install this plugin and then add below code to your file :

import NetInfo from '@react-native-community/netinfo';
...
...

class App extends Component {
    netInfoHandler;
    constructor(props) {
        super(props);
        this.state = {
            isConnected: true
        }
    }

    componentDidMount = () => {
        this.netInfoHandler = NetInfo.addEventListener(async (state) => {
            if (this.state.isConnected !== state.isConnected) {
                this.setState({ isConnected: state.isConnected });
                // show toast here
            }
        });
    }

    componentWillUnmount = () => {
        this.netInfoHandler();
    }

    render() {
        return (
            ...
            // your view
        )
    }
}

export default App
Kishan Bharda
  • 5,446
  • 3
  • 30
  • 57