WP Plugin Development with React.js | JSON & COVID-19 Data API
Creating simple React.js plugin and activating it
After development a plugin which used jQuery library for handling API requests and user interface creation I decided to create another one. This time I'd like to use one of the most popular JavaScript library on the market - React.js.
I took similar approach to previous plugin and created dedicated folder with functions.php file and imported required scripts from CDN (Content delivery Network) using wp_enqueue_script() WordPress method.
Had to import babel library to transpile JSX (JavaScript XML) into normal JavaScript code which can be used by the browser.
JSX allows to write HTML elements in JavaScript and place them in the DOM (Document Object Model) without the need of use createElement() and appendChild() methods - which I used with jQuery before.
Shortcode API was used to inject my React content in the required place. For that I created function displayReactPage() with echo and file_get_contents() methods to display html file in the web browser. Only thing left to do was to activate the plugin from wp-admin dashboard.
HTML file contains one div with id "root" all React content is rendered inside that using ReactDOM.render() method which takes what to render, in this case <App /> component and the destination - div grabbed by id using JavaScripts document.getElementById() function. It is treated as Single Page Application (SPA) because dynamic changes inside that container happens without reloading the whole page.
React plugin functionality development
Plugin utilises free API for the data on COVID-19 coronavirus. On load it displays Global (world) data then user can enter name of the country to get its data.
GET DATA button has onClick (executes when user click on it) function attached, which checking content of the input (stored in inputData variable) if empty (no data entered) it sets variable setInputError to true... which generates above error message. If all is alright it stores the input data (which is send in the request), and reset content of the input.
Data is grabbed using JavaScript Fetch API and fetch() method, then stored in the variable which is overwritten every time when the new request is made.
Sometimes New Cases and New Deaths data is not updated and return as empty values I decided to add "No Data" text in case this information is not provided at the time of request and provide of collapsing (empty) output fields. It is checked by if statement and length of the returned fields if not 0 then it will be overwritten by the actual data if 0 then stays as "No Data" text.
User Interface
User interface is generated when covidData variable has required data fetched. If there is some network error or bad request then warning screen is generated.
I store other data as a state using useState() method. This lets React's render parts of the user interface accordingly.
State variable country held data for creating HTTP request. It is interpolated as a last part of the URL string.
Rest of the state variables are used as a dynamically changing data injected with ${name of variable here} syntax inside the HTML template created with JSX
Comments
Post a Comment