Tag Archives: React Redux

https://res.cloudinary.com/dpdbfabms/images/w_624,h_350/f_auto,q_auto/v1651574445/UI-structure-image-1/UI-structure-image-1.png?_i=AA

React Redux – The art of state management

React is one of the most popular technologies for front-end development, which uses a component based approach to enable fast app development. Redux is a predictable state container for JS apps, and works particularly well with React.  In this article, I will be covering some basics on how to set up React-Redux for state management of a UI.

To illustrate the advantages of this approach, have a look at the UI structure shown below.

UI structure image 1

Let’s say the functionality required is that when a user interacts with the big component, the application has to update the small component and the header. In other words, we need to sync the header and small component as well as manage the states of all the other components. Without a state management library such as the one provided by Redux, coding this implementation can easily become very complicated.

Using the state functionality in React allows only the use of local, component level states.

Image 2

You can store the states using setState method, and fetch the stored state using state.[STATE_NAME]. However, these states are local, and only available within the class component in which it is declared.

Setstate image3

To store the states at the application level, i.e. the global level, in order to access them from different components, we can use React-Redux.

Let’s see how React-Redux works

You will see three main elements in the Redux architecture – Store, Dispatch Actions and Reducers.

Redux architecture 4

Store: holds the states at the application level, and allows any component to get data from the store in the form of Redux States.

Dispatch Action: a function of the Redux Store to trigger a state change.

Reducers: It handles the value change in Redux State.

How to configure React Redux in a React JS project –

The following packages are needed to be installed to configure  Redux in the project –

  1. Redux: the core package of React Redux.
  2. React-redux: needed for React bindings.
  3. Redux-thunk: a middleware which allows you to call action creators, and which returns a function instead of the action object.

These dependencies can be installed by using the following NPM command –
npm install redux react-redux redux-thunk –save

After these dependencies are installed, a store can be created and initialized with data/null values as per the requirements. This can be done by creating the file store.js in the project.

First however, you need to create reducer.js to handle the next states. Below is the example of reducer.js to initialize the state. Here, it is just returning the initial value from the reducer.

reducer.js 5

Below is a slightly more complex example of the code in reducer.js to update the states based on the action executed from the component.

coding 6

In the above example, it adds the data to the state upon the dispatch action (ADD_DATA). Now let’s create the action to dispatch. You can create a file with the name action.js, that contains the code shown below.

action.js 7

Once the action is created, now you can create a store and then connect the store to the application. You can create a file with the name store.js, that contains the code shown below.

store.js 8

To connect the store to the application, you will have to use the entry point. In the index.js (i.e. entry point of the app), import the store as shown below –

coding 9

These are all the Redux configurations for an application. Refer the the image below to use it in your component –

React component 10

React Redux can handle large amounts of data at the application level with cleaner components and is particularly useful for big applications.

Without Redux 11

With Redux 12

Without redux, to transfer data from component 1 to component 5, you need to pass it through middle components. This increases the props for components and creates more complexity and the components are less clean.

Using React-Redux, you can get or set the data to the store from any of the components directly by mapping the redux states to component props. In this case, you do not need to pass props to the middle components. This allows for much cleaner components and increased clarity in the code.

If you are looking to build a mobile or web application using React technology, then please feel free to contact us.

https://res.cloudinary.com/dpdbfabms/images/w_624,h_413/f_auto,q_auto/v1651575058/Access-tokens-1-4/Access-tokens-1-4.png?_i=AA

Six smart steps to configure Mapbox with ReactJS

Mapbox provides custom maps, search and navigation. It supports many platforms. The tool is powered by programmers as well as millions of devices providing Location Data. It provides developers a platform and a toolset to build customized applications where location-based data can be used to meet a variety of business requirements.
A typical scenario where Mapbox is used:
A service provider uses Mapbox to track locations of their assets or employees in order to provide real-time information to customers or for operational monitoring. This could be a restaurant delivery service, an international transport company or even a fleet operator in a city.
Customizing Mapbox
One of our clients provides data services to monitor the health of agricultural farms. They wished to offer compelling data visualization of agricultural farm images along with other details. We customized mapbox to render this information on the web and mobile devices. There are SDKs available for the Web, iOS and Android.
In this article, we share how we configured mapbox in React JS using custom style to add new fonts, custom icons and publish these styles.Following are the steps involved –
1. Steps to generate the token number and style in Mapbox

  • a. Create an account on https://account.mapbox.com
  • b. Log in to your account
  • c. On the Dashboard look for the Default public token. This Token number can be used for configuration in the Web App. You need to remember that this token number is public and easily accessible from the developer tab.
  • d. To get the private Token click on the button ‘Create a Token’ you will get a form. Enter the ‘Token name’ and select the desired ‘Public Scope’ and the ‘Secret Scopes’.
    Access tokens
  • e. Note: – This token will only be accessible from the specific URLs that we add while generating the Token.
    Create an access tokenToken Restrictions
  • f. Now add the list of comma-separated URLs into the URL field and click on the ‘Create Token’. Next you will be asked to confirm with your account password.
  • g. You will find the private token at the bottom of the Token list.Private token

2. Steps to configure React JS app with Mapbox

  • a. There are two different methods available for using the Mapbox on the web App.
    We describe the steps using the ‘npm’ module bundler for installing Mapbox GL JS.Method selection for installing Mapbox
  • b. Install the ‘npm’ package –> ‘npm install mapbox-gl –save’
  • c. Include the GL JS CSS file
  • d. Include the CSS file in theof your HTML file
    • i.<link href=’https://api.mapbox.com/mapbox-gl-js/v1.3.1/mapbox-gl.css’ rel=’stylesheet’ />
  • e. Add the map to your Web App.
    • i. var mapboxgl = require(‘mapbox-gl/dist/mapbox-gl.js’);
    • ii. mapboxgl.accessToken = ‘Your Token number’;
    • iii. var map = new mapboxgl.Map({
    • iv. container: ‘YOUR_CONTAINER_ELEMENT_ID’,
    • v. style: ‘mapbox://styles/mapbox/streets-v11’ // Default style link
    • vi. });
  • f.  Now you are done with the configuration.

3. Steps to create the custom style of Mapbox

  • a.To create our custom style for the map, we need to open the Mapbox Studio,
  • b.To open the Mapbox studio, you can to go to the Dashboard and then click on the ‘Design in Mapbox Studio’ on the right side of the Dashboard or Just click on the link here https://studio.mapbox.com/
  • c.After opening the Mapbox studio, you can see the predefined styles you can use directly or you may choose to create a new style.Mapbox styles
  • d. To create a new style, click on the ‘New style’ button on top of the page.
  • e. Select the desired template from the list and can change the style of the map from bottom of the dialog and then click on the customize button.Templates
  • f. You can see a dashboard with different settings.
  • g. You can update the style of the country label, background, road, buildings and many moreUpdating styleicons

4. Steps to add a new font to the map and then publish for production

  • a. Click on the Fonts tab on the top-right corner then you can find a dialog to upload the new fonts.Upload fonts
  • b. Clicking on the Upload new font, you can see a new dialog with the title ‘Upload fonts’ here you can drag new fonts with extension ‘.TTF’ and ‘.OTF’.
    Upload new font
  • c. Once Font family is added, it can be then used in the map.icons

5. Steps to add a custom icon or an image and publish

  • a. The image we upload here will be available as an icon on the map for example Image as a custom marker.
  • b. Click on the ‘Images’ tab in the header you can see a dialog to upload the SVG image.
    Upload svgDrag and drop svg images
  • c. To upload the SVG image, click on the ‘Upload SVG Icon’ button, now you can see a new dialog where you can drag your SVG image.
  • d. Once you update any style, you have to publish the style to see the changes on the map.
  • e. Note: – It may take some time to get published and to reflect on the map.
  • f. It is very easy to publish the changes; you just have to click on the ‘Publish’ button on the top-right corner of the map box studio.
    Image
  • g. Now you can see a dialog with a slider where you can see the styles you added.
  • h. Here you can either ‘Publish as new’ which will replace all the styles, or, use the ‘Publish’ button to add the styles you added in your draft.
    Basic template

6. Steps to add the updated style to our map

  • a. Once the style is published,click on the ‘Share’ tab on the top-right corner of the Mapbox Studio. Here you can see a dialog where you can find your ‘Your style Url’ and ‘Your access token’.
    Share
  • b. Now update your style URL of the map.
    • i. var map = new mapboxgl.Map({
    • ii. style: ‘Add your style URL Here’
    • iii. });
  • c. Example:
    • i. const feature: feature = {
    • ii. id: 123,
    • iii. type: ‘Feature’,
    • iv. geometry: feature.geometry,
    • v. properties: {
      • 1. meta: feature.meta,
      • 2. icon: ‘square-stroke’
      • 3. // Custom Icon name which is added to the style in Mapbox Studio
    • vi. }
    • vii. }

We at MetaSys software have helped the clients in building such kind of custom software solutions. For more details – https://www.metasyssoftware.com/case-study-react