I've just managed to fix this problem after about two days of trying and as I know alot of people sooner or later get this issue I wanted to share what worked for me.
Firstly I moved as close to my router as possible . Then I disconnected the joycons. Then I reset the console by holding down the power button for 15 seconds and rebooting. Then using the touch controls I entered the system settings. I then entered the internet settings. I selected my WiFi and attempted to connect but got the message saying that it was currently unable too. I went to change settings and changed the security to WPA2-PSK ( if this is already selected change to WPA2-PSK). Then I changed IP address settings and DNS settings to manual. I didn't enter anything but then went back into them and changed them back to automatic.
When I tried to reconnect it connected fine and has stayed connected since.
I'm not sure all these steps are required but this is what has worked for me and hopefully will for others too.