{"id":56273,"date":"2022-12-29T12:28:09","date_gmt":"2022-12-29T06:58:09","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=56273"},"modified":"2023-01-02T09:29:01","modified_gmt":"2023-01-02T03:59:01","slug":"loosely-coupled-pass-data-to-react-app-via-drupalsettings","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/loosely-coupled-pass-data-to-react-app-via-drupalsettings\/","title":{"rendered":"Loosely Coupled: Pass data to React app via drupalSettings"},"content":{"rendered":"<p>There may be a need to pass a variable, checkbox settings, or some global settings from drupal to react. This can be achieved by passing data from drupal into an API and consuming the same API in react. But why call an extra API when we can achieve it without it?<br \/>\nWe will be using #attached\/drupalSettings to accomplish this.<\/p>\n<h3>Step 1: Modify our library definition.<\/h3>\n<p>Add dependency for drupalSettings where react bundle file is included:<\/p>\n<pre>react-lib:\r\n  version: 1.x\r\n  js:\r\n    js\/react-app\/dist\/app.bundle.js: { minified: true }\r\n  dependencies:\r\n    - core\/drupalSettings<\/pre>\n<h3>Step 2: Pass settings to the browser using the #attached framework.<\/h3>\n<p>This can be done either in following ways:<\/p>\n<h4>i) Any Preprocess hook:<\/h4>\n<pre>function react_module_preprocess_node(&amp;$variables) {\r\n  $variables['content']['#attached']['drupalSettings']['reactApp']['setting1'] = 'value1';\r\n  $variables['content']['#attached']['drupalSettings']['reactApp']['setting2'] = 'value2';\r\n}<\/pre>\n<h4>ii) Page attachment hook:<\/h4>\n<pre>function react_module_page_attachments(array &amp;$attachments) {\r\n  $attachments['#attached']['library'][] = 'react_module\/react-lib';\r\n  $attachments['#attached']['drupalSettings']['reactApp']['setting1'] = 'value1';\r\n  $attachments['#attached']['drupalSettings']['reactApp']['setting2'] = 'value2';\r\n}<\/pre>\n<h4>iii) Render array (Forms, Block etc).<\/h4>\n<p>For Instance, if you want to do things from a <strong>form alter<\/strong>, where we have added the markup and the library:<\/p>\n<pre>$form['my_react_app'] = [\r\n  '#markup' =&gt; '&lt;div id=\"react-root\"&gt;&lt;\/div&gt;',\r\n  '#attached' =&gt; [\r\n    'library' =&gt; [\r\n      'react_module\/react-lib'\r\n    ],\r\n    'drupalSettings' =&gt; [\r\n      'reactApp' =&gt; [\r\n        'setting1' =&gt; 'value1',\r\n        'setting2' =&gt; 'value2',\r\n      ],\r\n    ],\r\n  ],\r\n];<\/pre>\n<h3>Step 3: Access settings in React App<\/h3>\n<p><strong>drupalSettings<\/strong> is a global variable so it can be used directly within the React app.<\/p>\n<pre>import React from 'react';\r\nimport ReactDOM from 'react-dom';\r\n\r\nconst root = ReactDOM.createRoot(document.getElementById('react-root'));\r\nroot.render(\r\n  &lt;&gt;{drupalSettings.reactApp.setting1} {drupalSettings.reactApp.setting2}&lt;\/&gt;\r\n);<\/pre>\n<p><strong>Note<\/strong>: If any of the changes are not reflected on page refresh, try <strong>clearing browser or Drupal cache<\/strong>. You can also always call <strong>console.log (drupalSettings);<\/strong> in the browser console to debug.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>There may be a need to pass a variable, checkbox settings, or some global settings from drupal to react. This can be achieved by passing data from drupal into an API and consuming the same API in react. But why call an extra API when we can achieve it without it? We will be using [&hellip;]<\/p>\n","protected":false},"author":1124,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":95},"categories":[3602,3038],"tags":[4862,5070,5067,5068,4064],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56273"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1124"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=56273"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56273\/revisions"}],"predecessor-version":[{"id":56309,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56273\/revisions\/56309"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=56273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=56273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=56273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}