This document provides the guidelines for building an e-Business control using React and consists of below sub-topics.

Refer to Guidelines for building or extending e-Business 6.0 Controls for controls built using Knockout .js

Prerequisite Knowledge

Key changes in the e-Business 7.0 Frontend implementation

Getting Started

  1. Installation of Node.js and npm (Node Package Manager)

    1. React development requires Node.js to be installed on your environment. It can be downloaded from the following link: - https://nodejs.org/en/download/

    2. npm comes bundled with the Node.js installable after v0.6.3 version. To verify the same, open command prompt and type the following command and see the result: npm --version

  2. Installation of React, React DOM, babel-cli, babel-preset-react and babel-plugin-transform-react-jsx

    1. The front-end project in the e-Business 7.0 setup includes the package.json file that contains a list of dependencies for React development.

    2. Open the Command Prompt and switch to the directory that holds the e-Business front-end project

    3. Run the following command to install all necessary dependencies mentioned in the package.json file: npm install

    4. Alternatively, run the following commands to manually install all dependencies

      1. Create package.json file: npm init -y

      2. Install react and react-dom : npm install react react-dom --save

      3. Install Babel : npm install babel-cli babel-preset-react babel-plugin-transform-react-jsx

Control level guidelines with examples.

<div class="ebBody ebWrapper">
    <div id="committeeListing"></div>
</div>

const { useState, useEffect } = React;

const [committeeList, setCommitteeList] = useState(null);

useEffect(() => {
  .................
  Action to perform
  .................
  }, [optionalState]);
const ControlName = () => {
    ........
    ........
    ........
    
    return (
      ..HTML..
      ..Code..
    );
};

React requires that the name of the component is in PascalCase, that is, the first letter of each word needs to be capitalized to avoid errors.

useEffect(() => {
        eb_UserContext.getContextData(true).done(function (userData) {
            eb_UserContext.live = new eb_UserContext.model(userData);
            if (eb_UserContext.live.isUserLoggedIn()) {
                eb_shoppingCart.getShoppingCart(eb_UserContext.live.LinkId()).done(function (cartData) {

                    var cartOptions = {};
                    cartOptions.shoppingCartData = cartData;
                    eb_shoppingCart.live = new eb_shoppingCart.shoppingCartModel(cartOptions);

                    //load footer control
                    var footerOptions = footerOptions || {};
                    footerOptions.domElement = eBusinessJQObject('#ebFooter')[0];
                    footerOptions.templatePath = eb_Config.SitePath + "html/Footer.html";
                    eb_Footer.render(footerOptions).done(function () {
                        //load header control
                        var headerOptions = headerOptions || {};
                        headerOptions.domElement = eBusinessJQObject('#ebHeaderMenu')[0];
                        headerOptions.templatePath = eb_Config.SitePath + "html/HeaderMenu.html";
                        headerOptions.userContext = eb_UserContext.live;
                        headerOptions.shoppingCart = eb_shoppingCart.live;
                        headerOptions.activePage = "committeeListing";
                        headerOptions.sitePath = "../";
                        eb_HeaderMenu.render(headerOptions).done(function () {
                            eb_HeaderMenu.live = new eb_HeaderMenu.model(headerOptions);
                            ko.applyBindings(eb_HeaderMenu.live, headerOptions.domElement);/*Apply KO bindings, fire up the control*/

                            /*Fetch call to retrieve list of committees.*/
                            fetch(eb_committeeListing.getCommitteeListService, {
                                method: 'get',
                                credentials: 'include'
                            })
                                .then(response => {
                                    if (!response.ok) {
                                        throw Error(eb_committeeListing.defaultErrorMessage);
                                    }
                                    return response.json();
                                })
                                .then(data => {
                                    setCommitteeList(data);
                                    setCommitteeListToShow(data);
                                    setIsLoading(false);
                                    setError(null);
                                })
                                .catch(err => {
                                    setIsLoading(false);
                                    setError(err.message);
                                })

                        }).fail(function (data, msg, jhr) {
                            console.error('Failed to render header control...');
                            eb_Config.getErrorMessageForControl(data.responseJSON, eb_committeeListing);
                        });
                    }).fail(function (data, msg, jhr) {
                        console.error('Failed to render footer control...');
                    });
                }).fail(function (data, msg, jhr) {
                    console.error('Failed to getShoppingCart...');
                    eb_Config.getErrorMessageForControl(data.responseJSON, eb_committeeListing);
                });
            }else {
                (window.location.assign(eb_Config.loginPageURL));
            }
        }).fail(function (data, msg, jhr) {
            console.error("Failed to get user context data.");
        });

    }, []);
return (
        <div className="committeeListing">
            ....................
            HTML code
            ....................
        </div>
);
const root = ReactDOM.createRoot(document.getElementById('committeeListing'));
root.render(<CommitteeListing />);
fetch(eb_committeeListing.getCommitteeListService, {
                                method: 'get',
                                credentials: 'include'
                            })
                                .then(response => {
                                    if (!response.ok) {
                                        throw Error(eb_committeeListing.defaultErrorMessage);
                                    }
                                    return response.json();
                                })
                                .then(data => {
                                    setCommitteeList(data);
                                    setCommitteeListToShow(data);
                                    setIsLoading(false);
                                    setError(null);
                                })
                                .catch(err => {
                                    setIsLoading(false);
                                    setError(err.message);
                                })

Post JSX development

Once the development of the control has been done in JSX, follow the steps below to transpile the JSX file into JS:

  1. Open Command prompt and navigate to the directory that holds the e-Business front-end project.

  2. Run the following command

C:\LatestReact>node_modules\.bin\babel --plugins transform-react-jsx jsx_directory_path -d js_directory_path

For example,

C:\LatestReact>node_modules\.bin\babel --plugins transform-react-jsx jsx/committees -d js/committees

jsx/committees, is the directory that holds all the developed JSX files for the committees feature.

js/committees, is the directory where the newly created JS files are placed automatically post JSX transpiling