Have you ever wondered how to use React Context API? This tutorial will show you how to use React Context API to build a simple multilingual website. We will start with creating navigation. Then, we will build 3 simple pages. Finally, we will add 4 translations to make our website multilingual!
Have you ever wondered how to use React Context API? This tutorial will show you how to use it to build a simple multilingual website. In this part, we will start by creating navigation for our website. Then, we will build three simple pages. After that, we will prepare files for translation and make our website multilingual. I hope you will enjoy it. Now, let’s begin.
How to Use React Context to Build a Multilingual Website Part 1.
react-context-multilingual-website ├── node_modules/ ├── public/ ├── src/ │ └── app/ │ └── components/ │ └── header.jsx │ └── pages/ │ └── About.jsx │ └── Home.jsx │ └── Portfolio.jsx │ └── styles/ │ └── components/ │ └── header.css │ └── links.css │ └── pages/ │ └── about.css │ └── base.css │ └── grid.css │ └── layout.css │ └── Main.jsx │ └── translations/ │ └── cz.json │ └── de.json │ └── en.json │ └── fr.json │ └── context.jsx │ └── index.js │ └── registerServiceWorker.js ├── .env ├── package.json └── yarn.lock
We finished the previous part by creating
Main component, inside
Main.jsx file. As a result, we have basically everything we need to start working on our website. React Context is prepared and routes are set. Probably the best way to continue will be by creating the
Header component for our website. There are several reasons why we should start with
Reason number one is that
Header will be a relatively simple component, if we look away from the React Context for a moment.
Header component will contain a list with three separate
list items with links, one for each page. Under these links will be one additional
list item wrapping four links, one for each language variation of our website. Finally, there will be a menu button.
The reason number two is that
Header component will allow us to test React Context as well as translations for our website. As you may remember, we passed React Context to
Header component as
context prop, in
Main.jsx. We will use this props now for a couple of things. First, we will use it to show and hide the navigation with
isNavOpen key stored in
toggleNav method, both provided by React Context.
And, second, we will use the
changeLanguage to switch between language variations of our website when visitor clicks on one of the links dedicated to language.
Now, let’s quickly discuss how will the translations work. This will be simple. We will use
if statements to check the
language key stored inside the
state of React Context, passed through
context prop. We will compare the value of the
language key with three strings, ‘cz’, ‘de’ and ‘fr’. If match is found, we will load specific string from translation JSON file.
We will use English as default language. So, when there is no match with any of the strings, our website will switch to English. We will use this exact mechanism to switch between strings variations, when the visitor decides to the language, on the rest of our website. The only difference is that the rest of the website will use
Consumer component from React Context, not React
Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.
Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...
Hire dedicated React Native developers for your next project. As the top react native development company we offer you the best service as per your business needs.
Are you looking to [hire React JS developer](https://www.mobiwebtech.com/react-js-development-company/ "hire React JS developer") from a prestigious and reliable React JS development company? Visit at Mobiweb Technologies here we have a big team...
React Native is undoubtedly one of the most widely used cross-platform frameworks for creating native-like apps. This framework can be easily used for developing brand-new apps from scratch and even in existing iOS or Android projects.easily used for developing brand-new apps from scratch and even in existing iOS or Android projects.