Responsive Countries API with react.js and tailwind css with dark mode
Design comparison
Solution retrospective
My thoughts: So although I've managed to complete the challenge, there were definitely some challenges in the way that I was not able to solve.
One of them was working with the API. It was such a pain. This API's data fields are not consistent throughout the countries. Some don't have capital, some don't have native names, etc. I was constantly getting errors in my application.
Also, I don't know how to display "unknown" elements inside an object in the HTML structure. Like we can use the map function for arrays, but I don't know how to display items if they are inside an object. Specifically, some fields like languages and currencies were inside an object.
So if someone knows how to solve these problems, guidance would be appreciated.
Edit: fixed the "displaying object children" issue!
Community feedback
- @michagodfreyPosted over 1 year ago
Hi Asfer,
Great work on the challenge! It looks good and works well despite the difficulties you mentioned.
It sounds like you were having problems with rendering the data, I had a look at your code and I think you could try ternary operators to render the data. For example.
<p> <b>Capital: </b> {capital ? capital : null} </p>Without that condition, my React app would crash whenever the component was fed an object without a capital field.
Hope that helps :)
Marked as helpful0@Asfer-devPosted over 1 year ago@michagodfrey thanks! I'll try that next time.
I just checked your solution and awesome work on that!
Btw I would like to know how you implemented the different icons for light and dark mode? I tried the ternary operator for rendering two different icons based on the active theme but it doesn't render after the first time.
0@michagodfreyPosted over 1 year ago@Asfer-dev That's strange it doesn't switch the icon, I had a look at your header component and I used the same method you did - with a ternary operator. One thing you could try is to instead of setting the state directly in the JSX, is to use a function instead. For example:
change: onClick={() => setDarkMode(!darkMode)}
to: onClick={toggleDarkMode}
and have the function somewhere to handle the toggle function toggleDarkMode() { setDarkMode(!darkMode) }
Sometimes setting state directly in the JSX leads to strange behaviour.
That's my best guess, if this solves it please let me know because I'd be interested to find out if that worked.
Marked as helpful0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord