{"id":48798,"date":"2022-03-21T08:32:09","date_gmt":"2022-03-21T08:32:09","guid":{"rendered":"\/tutorials\/?p=48798"},"modified":"2026-03-10T09:29:11","modified_gmt":"2026-03-10T09:29:11","slug":"wordpress-react","status":"publish","type":"post","link":"\/ca\/tutorials\/wordpress-react","title":{"rendered":"A beginner&#8217;s guide to WordPress react development projects"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>WordPress is a powerful content management system (CMS) that lets you build everything from simple sites to sophisticated and complex eCommerce stores. To integrate the platform&rsquo;s PHP code with JavaScript, you can leverage WP REST API and WordPress React.<\/p><p>Developed by Facebook, React is a <strong>user interface (UI)<\/strong> library that uses a simple, component-based approach for building scalable, cross-platform applications that are easy to understand. However, it&rsquo;s important to learn how to use it properly to make the most of its features and functions.<\/p><p>In this guide, we&rsquo;ll explain how to use React with WordPress. We&rsquo;ll discuss what the framework is, outline its benefits, and show you how to use it. Let&rsquo;s jump in!<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Mega-WordPress-Cheat-EN.pdf\" target=\"_blank\" rel=\"noopener\">Download all-in-one WordPress cheat sheet<\/a><\/p><p>\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-an-introduction-to-react\">An Introduction to React<\/h2><p>Before we get started, we first want to ask the question, <a href=\"\/ca\/tutorials\/what-is-react\">&ldquo;what is React?&rdquo;<\/a>. Also known as ReactJS, it&rsquo;s one of the most popular JavaScript libraries you can use for web development.<\/p><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/react.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"326\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/react.jpg\" alt=\"React JavaScript library.\" class=\"wp-image-48825\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Created and maintained by Facebook, it includes an extensive array of JavaScript code snippets that can be used for building UI components.<\/p><p>Contrary to popular belief, ReactJS is not a JavaScript framework since it only renders components of an application&rsquo;s view layer. Therefore, you can pair it with an actual framework like <a href=\"https:\/\/vuejs.org\/\" target=\"_blank\" rel=\"noopener\">Vue.js<\/a> if you&rsquo;re looking for more sophisticated functions.<\/p><p>It&rsquo;s also important to note that there&rsquo;s ReactJS and <a href=\"https:\/\/reactnative.dev\/\" target=\"_blank\" rel=\"noopener\">React Native<\/a>. The latter is an open-source JavaScript framework built on the React library. You can use it to create cross-platform apps and platform-specific components for iOS and Android.<\/p><h2 class=\"wp-block-heading\" id=\"h-react-features-and-functions\">React Features and Functions<\/h2><p>To understand the benefits of React, it&rsquo;s helpful to know how it works. Here are some of its most significant features and functions:<\/p><h3 class=\"wp-block-heading\" id=\"h-jsx\">JSX<\/h3><p>The main JavaScript syntax extension used in React is <a href=\"https:\/\/reactjs.org\/docs\/introducing-jsx.html\" target=\"_blank\" rel=\"noopener\">JSX<\/a>. You can use it to embed HTML code in JavaScript objects and simplify complex code structures.<\/p><p>JSX also helps prevent <a href=\"https:\/\/owasp.org\/www-community\/attacks\/xss\/\" target=\"_blank\" rel=\"noopener\">cross-site scripting (XSS) attacks<\/a> by making it difficult for third parties to inject extra code through user input that isn&rsquo;t explicitly written in the application.<\/p><p>JSX tags include a name, children, and attributes. A typical HTML image tag would look something like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;img class=\"\" src=\"\" alt=\"\" &gt;<\/pre><p>However, a JSX tag would look like the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;img className=\"\" src=\"\" alt=\"\" \/&gt;<\/pre><p>Also, the numeric values are written inside curly brackets. Similar to JavaScript, the quotation marks represent strings:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const name = 'John Doe&rsquo;;\nconst element = &lt;h1&gt;Hello, {name}&lt;\/h1&gt;;\n\nReactDOM.render(\n  element,\n  document.getElementById('root')\n);<\/pre><p>You can put any valid <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Guide\/Expressions_and_Operators#Expressions\" target=\"_blank\" rel=\"noopener\">JavaScript expression<\/a> inside the curly brackets. For example, it could be &ldquo;user.firstName&rdquo; or &ldquo;formatName(user)&rdquo;.<\/p><h3 class=\"wp-block-heading\" id=\"h-virtual-dom\">Virtual DOM<\/h3><p>The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document_Object_Model\/Introduction\" target=\"_blank\" rel=\"noopener\">Document Object Model (DOM)<\/a> presents a web page in a data tree structure, which React stores in its memory. React can implement updates to certain parts of the tree rather than re-render it entirely.<\/p><p>The <a href=\"https:\/\/reactjs.org\/docs\/dom-elements.html\" target=\"_blank\" rel=\"noopener\">Virtual DOM<\/a> offers one-way data binding. This makes manipulating and updating it quicker than the original DOM.<\/p><p>It uses a process known as diffing. This is when React generates a new Virtual DOM tree, compares it to the old one, then finds the most efficient way to apply changes to the real DOM. This takes less time and requires fewer resources, which is beneficial for large projects that involve a lot of user interaction.<\/p><p>The DOM also supports the declarative API. This means you can tell React which state you want the UI to be in to ensure that the DOM will match that state.<\/p><h3 class=\"wp-block-heading\" id=\"h-components\">Components<\/h3><p>As we mentioned, <a href=\"https:\/\/reactjs.org\/docs\/components-and-props.html\" target=\"_blank\" rel=\"noopener\">React components<\/a> are standalone, reusable pieces of code that make up the UI. These components work similarly to JavaScript functions. They accept props, which are arbitrary inputs. When a function component returns an element, it influences how the UI will look.<\/p><p>Props are read-only. Here&rsquo;s an example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import React, { useState } from 'react' \nimport ReactDOM from 'react-dom' \n \nconst ParentComponent = () =&gt; { \n    const [stateVariable, setStateVariable] = useState('this is the starting value for the variable'); \n \n    return ( \n        &lt;div&gt; \n            &lt;h1&gt;This is a function component view&lt;\/h1&gt;\n            &lt;ChildComponent exampleProp={stateVariable} \/&gt;\n        &lt;\/div&gt; \n    ) \n} \n \nconst ChildComponent = (props) =&gt; {\n    return (\n        &lt;div&gt;\n            &lt;h2&gt;{props.exampleProp}&lt;\/h2&gt;\n        &lt;\/div&gt;\n    )\n}\n \n \nReactDOM.render( &lt;ParentComponent \/&gt;, document.getElementById('app') );\n<\/pre><p>There are two main types &ndash; <strong>class<\/strong> <strong>components<\/strong> and <strong>functional<\/strong> <strong>components<\/strong>. Class components use lifecycle <a href=\"\/ca\/tutorials\/what-are-wordpress-hooks\/\">WordPress hooks<\/a> and API calls:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class ExampleComponent extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = { };\n  }\n  render() {\n    return (\n      &lt;div&gt;\n        &lt;h1&gt;This is a view created by a class component&lt;\/h1&gt;\n      &lt;\/div&gt;\n    );\n  }\n}<\/pre><p>Meanwhile, a functional component looks something like the example below:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const ExampleComponent = (props) =&gt; {\n    const [stateVariable, setStateVariable] = useState('');\n    return (\n        &lt;div&gt;\n            &lt;h1&gt;This is a function component view&lt;\/h1&gt;\n        &lt;\/div&gt;\n    )\n}<\/pre><p>Functional components are for rendering views without data requests or state management.<\/p><h3 class=\"wp-block-heading\" id=\"h-state\">State<\/h3><p><a href=\"https:\/\/reactjs.org\/docs\/state-and-lifecycle.html\" target=\"_blank\" rel=\"noopener\">State<\/a> refers to the built-in object of React components. This is where you store property values of elements. If the state changes, the component re-renders.<\/p><p>State management is the process of managing these application states and storing data in state management libraries. You can use a handful of state management libraries, including <a href=\"https:\/\/redux.js.org\/\" target=\"_blank\" rel=\"noopener\">Redux<\/a> and <a href=\"https:\/\/recoiljs.org\/\" target=\"_blank\" rel=\"noopener\">Recoil<\/a>, with the latter being more beginner-friendly.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-wordpress-hosting\" href=\"\/ca\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner-1024x300.png\" alt=\"\" class=\"wp-image-111781\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-why-use-react\">Why Use React?<\/h2><p>There are many benefits to using React for WordPress development. For starters, it&rsquo;s beginner-friendly, and all popular <a href=\"\/ca\/web-hosting\">hosting solutions<\/a> support its use.<\/p><p>Since it relies on plain JavaScript and components, you can use it to create web-based applications after only a few days of learning it. There are also plenty of websites to <a href=\"\/ca\/tutorials\/learn-coding-online-for-free\">learn how to code for free online<\/a>. A solid understanding of JavaScript basics can streamline the process.<\/p><p>Another benefit of React is that it lets you reuse components in other applications. It&rsquo;s open-source, so you can pre-build your components and nest them between others without bloating your code.<\/p><p>React components are also relatively straightforward to write, thanks to the JSX integration, which we&rsquo;ll discuss in a moment. You can incorporate HTML typography, tags and leverage multiple functions rendering for dynamic application development.<\/p><p>With React, you can also use the official command-line interface (CLI) &ndash; <strong>Create<\/strong> <strong>React<\/strong> <strong>App<\/strong> &ndash; to expedite single-page application development. It comes with pre-configured tools that can help you streamline the setup and learning process.<\/p><p>Finally, React is also SEO-friendly. The Virtual DOM implementation helps increase page speed, boosting performance and server-side rendering, making it easier for bots to crawl your site.<\/p><h2 class=\"wp-block-heading\" id=\"h-an-overview-of-wp-rest-api\">An Overview of WP REST API<\/h2><p><a href=\"\/ca\/tutorials\/wordpress-rest-api\">WordPress REST application program interface (API)<\/a> enables developers to integrate JavaScript frameworks such as React with WordPress. You can access WP REST API from the front-end of your site, add custom post types, and build a React app backed by this API.<\/p><p>WP REST API is a set of protocols used for building software applications. They define how information and data are shared between programs and how their components interact. REST, short for Representational State Transfer, refers to the style-defining architectural constraints of programs.<\/p><p>The format for structuring data to be read by applications is called <a href=\"\/ca\/tutorials\/what-is-json\">JavaScript Object Notation (JSON)<\/a>. It helps streamline communication between WordPress and other applications and programs.<\/p><p>WP REST API results in a decoupling environment that lets users treat <a href=\"\/ca\/tutorials\/headless-wordpress\">WordPress as a headless<\/a> CMS. This means that a variety of front-end frameworks can be used to hook the back end of WordPress. This is advantageous for developers who aren&rsquo;t overly enthusiastic about PHP.<\/p><h2 class=\"wp-block-heading\" id=\"h-react-before-getting-started\">React &ndash; Before Getting Started<\/h2><p>A basic knowledge of <a href=\"\/ca\/tutorials\/what-is-javascript\">JavaScript<\/a>, <a href=\"\/ca\/tutorials\/css-cheat-sheet\">CSS<\/a>, and <a href=\"\/ca\/tutorials\/html-cheat-sheet\">HTML<\/a><strong> <\/strong>would help to learn React. In addition, the learning process can be more efficient if you&rsquo;re familiar with <a href=\"http:\/\/es6-features.org\/\" target=\"_blank\" rel=\"noopener\">ECMAScript 6<\/a> (also known as ES6), functional programming, and object-oriented programming.<\/p><p>Program-wise, you&rsquo;ll also need a few dependencies installed on your computer. This includes <a href=\"\/ca\/tutorials\/what-is-node-js\">NodeJS<\/a> and <a href=\"\/ca\/tutorials\/what-is-npm\">npm<\/a> and a text editor. Optionally, you may also want to use <a href=\"https:\/\/git-scm.com\/downloads\" target=\"_blank\" rel=\"noopener\">Git<\/a> for version controlling.<\/p><p>The most popular way to develop React projects for WordPress is to use <a href=\"https:\/\/create-react-app.dev\/\" target=\"_blank\" rel=\"noopener\">Create React App<\/a>:<\/p><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/create-react-app.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"359\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/create-react-app.png\" alt=\"Create React App.\" class=\"wp-image-48826\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>CRA provides a simple environment for learning React and using it to build single-page applications. Note that in order to use it, you&rsquo;ll first need Node and npm on your device.<\/p><p>To create a new project, you can run the following command in your terminal:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npx create-react-app wp-react-demo<\/pre><p>If you haven&rsquo;t done so already, it will confirm that you want to install Create React App before creating the demo. This makes a boilerplate template. You can also replace &ldquo;wp-react-demo&rdquo; with your own name.<\/p><p>Next, run the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cd wp-react-demo\nnpm start<\/pre><p>The directory structure will look like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&#9500;&#9472;&#9472; README.md\n&#9500;&#9472;&#9472; package.json\n&#9500;&#9472;&#9472; public\n&#9474; &#9500;&#9472;&#9472; favicon.ico\n&#9474; &#9500;&#9472;&#9472; index.html\n&#9474; &#9492;&#9472;&#9472; manifest.json\n&#9500;&#9472;&#9472; src\n&#9474; &#9500;&#9472;&#9472; App.css\n&#9474; &#9500;&#9472;&#9472; App.js\n&#9474; &#9500;&#9472;&#9472; App.test.js\n&#9474; &#9500;&#9472;&#9472; index.css\n&#9474; &#9500;&#9472;&#9472; index.js\n&#9474; &#9500;&#9472;&#9472; logo.svg\n&#9474; &#9492;&#9472;&#9472; registerServiceWorker.js<\/pre><p>In the src directory, you&rsquo;ll find all the JavaScript files you&rsquo;ll be working on. Now, you can visit <strong>localhost:3000<\/strong> to load the <strong>index.html<\/strong><em> <\/em>file.<\/p><p>The<em> <\/em><strong>public\/index.html<\/strong><em> <\/em>file doesn&rsquo;t contain much. However, you can find the following line, which will be the starting point for your project:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div id=\"root\"&gt;&lt;\/div&gt;<\/pre><p>Under the<em> <\/em><strong>index.js<\/strong> file of the src directory, you&rsquo;ll find the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import React from 'react';\nimport ReactDOM from 'react-dom';\nimport App from '.\/App';\n \nReactDOM.render(&lt;App \/&gt;, document.getElementById('root'));<\/pre><p>This means it&rsquo;s rendering the <strong>App<\/strong> component, which you can find under <strong>src.\/App.js<\/strong>. You can replace this with your own HTML content. We can use HTML insider <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">render()<\/code> thanks to the JSX syntax extension.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-query-wp-rest-api-from-react\">How to Query WP REST API from React<\/h2><p>By default, you can make a <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">GET<\/code> request to use WP REST API on the front-end of your site. For example, you can use <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">\/wp-json\/wp\/v2\/posts<\/code> to get all your posts. Sticking with the previous example, this post data would be located in <strong>http:\/\/localhost\/wp-json\/wp\/v2\/posts\/<\/strong>.<\/p><p>To get started with React, you can run this command in your terminal:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npx create-react-app react-app<\/pre><p>Then, run the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cd react-app \nnpm install @material-ui\/core<\/pre><p>Next, you can enter the example below:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import React, { useEffect, useState } from 'react';\nimport Card from '@material-ui\/core\/Card';\nimport CardContent from '@material-ui\/core\/CardContent';\nimport Typography from '@material-ui\/core\/Typography';\nimport Grid from '@material-ui\/core\/Grid';\n    \nexport default function Posts() {\n    const [posts, setPosts] = useState([]);\n    useEffect(() =&gt; {\n        async function loadPosts() {\n            const response = await fetch('\/wp-json\/wp\/v2\/posts');\n            if(!response.ok) {\n                \/\/ oups! something went wrong\n                return;\n            }\n    \n            const posts = await response.json();\n            setPosts(posts);\n        }\n    \n        loadPosts();\n   }, [])\n  return (\n    &lt;Grid container spacing={2}&gt;\n      {posts.map((post, index) =&gt; (\n      &lt;Grid item xs={4} key={index}&gt;\n        &lt;Card&gt;\n           &lt;CardContent&gt;\n                &lt;Typography\n                    color=\"textSecondary\"\n                    gutterBottom\n                    dangerouslySetInnerHTML={{__html: post.title.rendered}} \/&gt;\n                &lt;Typography\n                    variant=\"body2\"\n                    component=\"p\"\n                    dangerouslySetInnerHTML={{__html: post.content.rendered}} \/&gt;\n            &lt;\/CardContent&gt;\n        &lt;\/Card&gt;\n      &lt;\/Grid&gt;\n     ))}\n    &lt;\/Grid&gt;\n );\n}<\/pre><p>The above example uses the <a href=\"https:\/\/reactjs.org\/docs\/hooks-overview.html\" target=\"_blank\" rel=\"noopener\">React Hooks<\/a> <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">useEffect<\/code> and <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">useState<\/code>. The former declares the array of posts and calls to update it, whereas the latter fetches the code.<\/p><p>    <p class=\"warning\">\n        <b>Warning!<\/b> When using <b>dangerouslySetInnerHTML<\/b> in your code, be aware of the following risks:<br>\n1. <b>Security Vulnerabilities<\/b>: Improper usage can expose your application to <b>cross-site scripting (XSS)<\/b> attacks.<br>\n2. <b>Code Maintainability<\/b>: It becomes harder to maintain and debug code when HTML is injected directly.    <\/p>\n    \n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-create-a-custom-post-type-with-react\">How to Create a Custom Post Type With React<\/h2><p>You can also use React to create a <a href=\"\/ca\/tutorials\/wordpress-custom-post-types\">WordPress custom post type<\/a>. However, there are a few tools that you&rsquo;ll need to install ahead of time. This will help make the process as seamless and straightforward as possible.<\/p><p>First, you&rsquo;ll need to add the <a href=\"https:\/\/wordpress.org\/plugins\/custom-post-type-ui\/\" target=\"_blank\" rel=\"noopener\">Custom Post Type UI<\/a> WordPress plugin.<\/p><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/custom-post-type-plugin.png\"><img loading=\"lazy\" decoding=\"async\" width=\"922\" height=\"293\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/custom-post-type-plugin.png\" alt=\"Custom Post Type UI plugin.\" class=\"wp-image-48828\"  sizes=\"auto, (max-width: 922px) 100vw, 922px\" \/><\/a><\/figure><\/div><p>This plugin simplifies the process of creating custom post types in WordPress.<\/p><p>We also recommend installing <a href=\"https:\/\/wordpress.org\/plugins\/advanced-custom-fields\/\" target=\"_blank\" rel=\"noopener\">Advanced Custom Fields (ACF)<\/a>.<\/p><figure class=\"wp-block-image size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/advanced-custom-field.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"332\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/advanced-custom-field.jpg\" alt=\"Advanced Custom Field WordPress plugin.\" class=\"wp-image-48829\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><p>This is also a free tool. The plugin can be used to create and add custom fields to your custom post types. We also suggest installing <a href=\"https:\/\/wordpress.org\/plugins\/acf-to-rest-api\/\" target=\"_blank\" rel=\"noopener\">ACF to REST API<\/a> to make your custom fields available to your post types.<\/p><p>To get started, navigate to<em> <\/em><strong>CPT UI &gt; Add\/Edit Post Types<\/strong><em> <\/em>from your admin area. In our example, we&rsquo;ll use the name &ldquo;Books&rdquo;. We&rsquo;ll also select the option to auto-populate the rest of the fields.<\/p><div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/wordpress-edit-post-types.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"550\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/wordpress-edit-post-types-1024x550.png\" alt=\"The add\/edit a new post type section in the WordPress dashboard. \" class=\"wp-image-48831\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Under the <strong>Show in REST API<\/strong><em> <\/em>section, set it to <strong>True<\/strong> and enter &ldquo;Books&rdquo; as the REST API base slug. Under <strong>Supports<\/strong>, select the <strong>Author<\/strong><em> <\/em>and<em> <\/em><strong>Custom fields<\/strong><em> <\/em>checkboxes. Click on <strong>Save Post Type<\/strong> when you&rsquo;re done.<\/p><p>Next, we can create custom fields for the custom post type. Navigate to <strong>Custom Fields &gt; Add New<\/strong>, and enter an appropriate title, such as &ldquo;Book Info&rdquo;. You can click on <strong>Add Field,<\/strong><em> <\/em>then complete the field label, name, and type.<\/p><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/wordpress-add-field.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"506\" src=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/03\/wordpress-add-field.png\" alt=\"Adding a new field group in WordPress dashboard.\" class=\"wp-image-48832\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Under<em> <\/em><strong>Location<\/strong>, you can set the post type to equal the<em> <\/em><strong>book<\/strong> custom post type. When you&rsquo;re done, select <strong>Publish<\/strong> to activate the custom field.<\/p><p>Next, go to <strong>My Books &gt; Add New<\/strong><em> <\/em>and enter its title, summary, and featured image. You can repeat this process to create as many as you&rsquo;d wish.<\/p><p>Now, from your terminal, enter the following command, running each one before moving on to the next:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npx create-react-app frontend\ncd frontend\nnpm i axios\nnpm start<\/pre><p>Next, within the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">src<\/code> directory, you can create a folder called <em>components<\/em>, followed by a file called <strong>books.js<\/strong>. When you&rsquo;re done, enter the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import React, { Component } from 'react';\nimport axios from 'axios';\nexport class Books extends Component {\n   state = {\n       books: [],\n       isLoaded: false\n   }\n componentDidMount () {\n   axios.get('http:\/\/localhost:3000\/wp-json\/wp\/v2\/books')\n       .then(res =&gt; this.setState({\n           books: res.data,\n           isLoaded: true\n       }))\n       .catch(err =&gt; console.log(err))\n   }\n   render() {\n      console.log(this.state);\n       return (\n           &lt;div&gt;\n             \n           &lt;\/div&gt;\n       )\n   }\n}\nexport default Books;<\/pre><p>In the<em> <\/em><strong>App.js<\/strong><em> <\/em>file, enter the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import React from 'react';\nimport '.\/App.css';\nimport Books from '.\/components\/Books';\nfunction App() {\n return (\n   &lt;div className=\"App\"&gt;\n    &lt;Books&gt;\n   &lt;\/div&gt;\n );\n}\nexport default App;<\/pre><p>Now, in your terminal, you can run the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">npm start<\/code> command. This will launch the React app. You can view the &ldquo;Book&rdquo; data console when you open it in a browser. You can display each title by mapping through each book.<\/p><p>In <strong>Book.js<\/strong>, enter the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">render() {\n      const {books, isLoaded} = this.state;\n       return (\n           &lt;div&gt;\n              {books.map(book =&gt; &lt;h4&gt;{book.title.rendered}&lt;\/h4&gt;)}\n           &lt;\/div&gt;\n       )\n   }<\/pre><p>Note that you can also separate the collection of books into a single book component. You can do this by creating a file within <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">src\/components<\/code>. You can also add custom CSS to style the custom post type (app.css).<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-create-a-react-wordpress-theme-2-methods\">How to Create a React WordPress Theme (2 Methods)<\/h2><p>Perhaps you&rsquo;re wondering <a href=\"\/ca\/tutorials\/create-wordpress-theme-html5\">how to create a WordPress theme<\/a> using React. There are a handful of options, so let&rsquo;s look at two popular methods:<\/p><h3 class=\"wp-block-heading\" id=\"h-1-create-a-react-wordpress-theme-using-wp-scripts\">1. Create a React WordPress Theme Using WP Scripts<\/h3><p>In typical React apps, you need to import React at the top of the files. However, as of <a href=\"https:\/\/make.wordpress.org\/core\/2018\/12\/06\/javascript-packages-and-interoperability-in-5-0-and-beyond\/\" target=\"_blank\" rel=\"noopener\">WordPress 5.0<\/a>, the CMS ships with React and ReactDOM libraries and exports them into <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">wp.element<\/code>, a global window object.<\/p><p>If you&rsquo;ve used React before, you&rsquo;ve likely used Create React App to run your development server. However, this isn&rsquo;t necessary with WordPress.<\/p><p>The WordPress team created a <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">wp-scripts<\/code> package based on the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">react-scripts<\/code> used with Create React App. WP Scripts includes the commands by default.<\/p><p>To load React in your theme, navigate to your <strong>functions.php<\/strong> file of your theme, and add the following code snippet:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Enqueue Theme JS w React Dependency\nadd_action( 'wp_enqueue_scripts', 'my_enqueue_theme_js' );\nfunction my_enqueue_theme_js() {\n  wp_enqueue_script(\n    'my-theme-frontend',\n    get_stylesheet_directory_uri() . '\/build\/index.js',\n    ['wp-element'],\n    time(), \/\/ Change this to null for production\n    true\n  );\n}<\/pre><p>This will add the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">wp-element<\/code> as a dependency of your JavaScript file. Next, we need to set up a <strong>package.json<\/strong> file. From the root of your WordPress theme, run the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm init<\/pre><p>Once that&rsquo;s done, run the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm install @wordpress\/scripts --save-dev<\/pre><p>WP Scripts will be downloaded to your <strong>node_modules,<\/strong> so they&rsquo;re available to use in your command line. To map the WP Scripts to NPM scripts, you can browse to your <strong>package.json<\/strong> file, load the &ldquo;scripts&rdquo; settings, and replace them with this example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"name\": \"myreacttheme\",\n  \"version\": \"1.0.0\",\n  \"description\": \"My WordPress theme with React\",\n  \"main\": \"src\/index.js\",\n  \"dependencies\": {},\n  \"devDependencies\": {\n    \"@wordpress\/scripts\": \"^5.1.0\"\n  },\n  \"scripts\": {\n    \"build\": \"wp-scripts build\",\n    \"check-engines\": \"wp-scripts check-engines\",\n    \"check-licenses\": \"wp-scripts check-licenses\",\n    \"lint:css\": \"wp-scripts lint-style\",\n    \"lint:js\": \"wp-scripts lint-js\",\n    \"lint:pkg-json\": \"wp-scripts lint-pkg-json\",\n    \"start\": \"wp-scripts start\",\n    \"test:e2e\": \"wp-scripts test-e2e\",\n    \"test:unit\": \"wp-scripts test-unit-js\"\n  },\n  \"author\": \"\",\n  \"license\": \"1 GNU V2+, MIT\"\n}<\/pre><p>Next, you can run the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm start<\/pre><p>This will look for files imported from <strong>src\/index.js<\/strong><em> <\/em>and build them to <strong>build\/index.js<\/strong> whenever a file is changed.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-wordpress-hosting\" href=\"\/ca\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner-1024x300.png\" alt=\"\" class=\"wp-image-111781\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h3 class=\"wp-block-heading\" id=\"h-2-create-a-react-wordpress-theme-using-a-create-react-wptheme\">2. Create a React WordPress Theme Using a Create-React-WPTheme<\/h3><p>Another option is to use <a href=\"https:\/\/github.com\/devloco\/create-react-wptheme\" target=\"_blank\" rel=\"noopener\">create-react-wptheme<\/a> by devloco. This package is similar to create-react-app. However, it uses WordPress instead of webpack as the development server. You&rsquo;ll also have access to all the core functions, hooks, actions, filters, etc.<\/p><p>To set it up, you can start a terminal (or git bash) in your local WordPress installation, then type the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npx create-react-wptheme barebones<\/pre><p><strong>&ldquo;Barebones&rdquo;<\/strong> is the name of the theme. You can change it to whatever you want to call your theme.<\/p><p>You&rsquo;ll see a message informing you that the installation created a root folder and a &ldquo;react-src&rdquo; directory within it. This directory is important because it will contain your uncompiled code.<\/p><p>Next, we need to add the <strong>styles.css<\/strong><em>, <\/em><strong>index.php<\/strong>,<em> <\/em>and other necessary files to validate it. In the terminal, enter the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cd barebones\/react-src\nnpm run wpstart<\/pre><p>Now, when you navigate to <strong>wp-admin &gt; themes<\/strong>, you should see your new theme. Click on the <strong>Activate<\/strong> button, then return to the terminal and run the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm run wpstart<\/pre><p>This should automatically open a new browser tab. Note that the files located in the root folder (anything outside of react-src) should not be edited. They contain the compiled version of the code required for WordPress and React.<\/p><p>To create a new theme, you can use the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npx create-react-wptheme my_react_theme<\/pre><p>You can change the &ldquo;my_react_theme&rdquo; as you want. Next, you can enter the following command:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cd my_react_theme\/react-src\nnpm run start<\/pre><p>Remember to replace it with the same name you used in the previous step. This will set up the theme to be viewed in the admin area.<\/p><p>After you&rsquo;re done developing your theme, you&rsquo;ll need to use the build command to move it to production. To do this, open your command prompt, navigate to the <strong>react-src<\/strong> folder of your theme, and run the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npm run build<\/pre><p>This will optimize your files located within a folder. Then you can deploy it on your server.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-add-react-to-a-custom-page-template\">How to Add React to a Custom Page Template<\/h2><p>If you&rsquo;re wondering <a href=\"\/ca\/tutorials\/how-to-create-wordpress-child-theme\">how to create a WordPress child theme<\/a> using React, the process is relatively similar. You can add React to a custom <a href=\"\/ca\/tutorials\/wordpress-page-template\">page template<\/a>.<\/p><p>Let&rsquo;s start with a standard theme structure:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/build\n\/src\n\/-- index.js\nfunctions.php\npackage.json\npage-react.php\nstyle.css<\/pre><p>Next, you&rsquo;ll add the following code to your theme&rsquo;s <strong>functions.php<\/strong> file:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nadd_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );\nfunction my_theme_enqueue_styles() {\n \n  $parent_style = 'twentytwenty-style'; \n    \n  wp_enqueue_style( $parent_style, get_template_directory_uri() . '\/style.css' );\n    \n  wp_enqueue_style( 'child-style',\n    get_stylesheet_directory_uri() . '\/style.css',\n    [ $parent_style ],\n    time() \/\/For production use wp_get_theme()-&gt;get('Version')\n  );\n\n  wp_enqueue_script(\n    'my-theme-frontend',\n    get_stylesheet_directory_uri() . '\/build\/index.js',\n    ['wp-element'],\n    time() \/\/For production use wp_get_theme()-&gt;get('Version')        \n  );\n  \n}<\/pre><p>You&rsquo;ll also need to add code to your <strong>style.css<\/strong> file. Within it, copy and paste the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/*\n Theme Name:   Twenty Twenty Child\n Description:  Twenty Twenty Child Theme\n Author:       Your Name\n Author URI:   https:\/\/yourwebsite.com\n Template:     twentytwenty\n Version:      0.9.0\n License:      GNU General Public License v2 or later\n License URI:  http:\/\/www.gnu.org\/licenses\/gpl-2.0.html\n Text Domain:  twentytwentychild\n*\/<\/pre><p>Next, you can create a basic custom page template <strong>page-react.php<\/strong> with this example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;? php\n\/**\n * Template Name: React Template\n *\/\nget_header();\n?&gt;\n\n&lt;main id=\"site-content\" role=\"main\"&gt;\n\t&lt;article class=\"post-2 page type-page status-publish hentry\"&gt;\n\t\t&lt;?php get_template_part( 'template-parts\/entry-header' ); ?&gt;\n\t\t&lt;div class=\"post-inner thin\"&gt;\n\t\t\t&lt;div class=\"entry-content\"&gt;\t\t\t\t\n\n\t\t\t\t&lt;div id=\"react-app\"&gt;&lt;\/div&gt;&lt;!-- #react-app --&gt;\n\n\t\t\t&lt;\/div&gt;&lt;!-- .entry-content --&gt;\n\t\t&lt;\/div&gt;&lt;!-- .post-inner --&gt;\n\t&lt;\/article&gt;&lt;!-- .post --&gt;\n&lt;\/main&gt;&lt;!-- #site-content --&gt;\n\n&lt;?php get_template_part( 'template-parts\/footer-menus-widgets' ); ?&gt;\n&lt;?php get_footer(); ?&gt;<\/pre><p>You can now create a new page in WordPress and then select <strong>React Template<\/strong> as the page template.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-use-react-in-your-app\">How to Use React in Your App<\/h2><p>If you want to use React in an existing app, you can add its <a href=\"\/ca\/tutorials\/what-is-cdn\">content delivery network (CDN)<\/a> directly to your HTML file. To get started, navigate to the HTML page where you want to add React to get started.<\/p><p>Insert a blank <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">&lt;div&gt;<\/code> tag. This will create a container where you can render a component. For this example, let&rsquo;s say we&rsquo;re trying to create a button.<\/p><p>Next, insert three <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">&lt;script&gt;<\/code> tags on the page before the closing <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">&lt;\/body&gt;<\/code> tag. The first two will be used to load React, and the third will load your component code.<\/p><p>Now, in the same folder as your HTML page, create a new file and name it <strong>button.js<\/strong> (or whatever is relevant to the element you&rsquo;re adding). Within that file, copy and paste the following code:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">'use strict';\nconst e = React.createElement;\nclass Button extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = { clicked: false };\n  }\n  render() {\n    if (this.state.clicked) {\n      return 'You pressed this button.';\n    }\n    return e(\n      'button',\n      { onClick: () =&gt; this.setState({ clicked: true }) },\n      'Press Me'\n    );\n  }\n}<\/pre><p>This will build a button component that displays a message once it&rsquo;s clicked. To use the button in your HTML page, you can add the following code snippet to the bottom of the <strong>button.js<\/strong> file:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const domContainer = document.querySelector('#button_container');\nReactDOM.render(e(Button), domContainer);<\/pre><p>This calls back to the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">&lt;div&gt;<\/code> container you added to your HTML page. Then it renders the React button component.<\/p><p>You can also use React to create a blank React app from scratch. The best way to do so is to use the Create React App solution we discussed earlier. It can be helpful for single-page applications.<\/p><p>To get started, you can run this command in your terminal:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">npx create-react-app my-app<\/pre><p>Note that you can change &ldquo;my-app&rdquo; to whatever you want to name it. To run it, you can browse to the app folder by running the <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">cd my-app<\/code> command, followed by <code data-enlighter-language=\"generic\" class=\"EnlighterJSRAW\">npm start<\/code>.<\/p><p>The app will then run in development mode. You can view it in your browser at <strong>http:\/\/localhost:3000<\/strong>.<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Developing WordPress React projects can be a simple, straightforward process, even if you&rsquo;re a beginner. However, it&rsquo;s important to learn how the JavaScript framework works with the CMS and its REST API to do so successfully.<\/p><p>Fortunately, with a basic understanding of JavaScript, HTML, and CSS, you can learn to React in no time. As we discussed in this post, you can use it to create single-page applications and themes and custom page templates.<\/p><p>We hope this guide has helped you better understand React and its capabilities for web development. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>WordPress is a powerful content management system (CMS) that lets you build everything from simple sites to sophisticated and complex eCommerce stores. To integrate the platform&rsquo;s PHP code with JavaScript, you can leverage WP REST API and WordPress React. Developed by Facebook, React is a user interface (UI) library that uses a simple, component-based approach [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ca\/tutorials\/wordpress-react\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":76,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"WordPress React: A Complete Guide to Get You Started","rank_math_description":"Read on to learn how to build a dynamic WordPress React project \u2013 from learning the basics to building apps, themes, custom posts, and pages.","rank_math_focus_keyword":"wordpress react","footnotes":""},"categories":[22698,22692],"tags":[],"class_list":["post-48798","post","type-post","status-publish","format-standard","hentry","category-advanced","category-wordpress"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/wordpress-react","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/wordpress-react","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/react-wordpress","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/wordpress-react","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/panduan-wordpress-react","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/wordpress-react","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/a-beginners-guide-to-wordpress-react-development-projects","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/a-beginners-guide-to-wordpress-react-development-projects","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/wordpress-react","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/wordpress-react","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/wordpress-react","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wordpress-react","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/wordpress-react","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/wordpress-react","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/wordpress-react","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/wordpress-react","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/48798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/comments?post=48798"}],"version-history":[{"count":28,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/48798\/revisions"}],"predecessor-version":[{"id":138289,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/posts\/48798\/revisions\/138289"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/media?parent=48798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/categories?post=48798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ca\/tutorials\/wp-json\/wp\/v2\/tags?post=48798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}