Message Boards

Access Liferay-JavaScript-Object in React-Portlet

thumbnail
Fredi B, modified 3 Years ago.

Access Liferay-JavaScript-Object in React-Portlet

Junior Member Posts: 69 Join Date: 4/1/20 Recent Posts
Hello Liferay-Community,
is there some way to get access to the "Liferay-Object" in an only to Liferay adapted Create-React-App?
I want to access things like the authKey or Language-Keys  in my React Portlet.
​​​​​​​
For example: Liferay.Language.get('portlet-namespace')

Thank you and Happy Fathers-Day tomorrow to all Fathers,
Fredi
thumbnail
Fredi B, modified 3 Years ago.

RE: Access Liferay-JavaScript-Object in React-Portlet

Junior Member Posts: 69 Join Date: 4/1/20 Recent Posts
Whoops,
I guess I found (atleast one) solution.
 componentDidMount() { 
       let liferay = window['Liferay']; 
       console.log(liferay);     
}

Are there any other ways?
Maybe also a solution how to easily access the fields of the configuration created with configuration.json?

"portletInstance": {        
"fields": {            
"Position": {                
"type": "string",                
"name": "Position",                
"description": "Position of Text",               
 "default": "center",                
"options": {                   
 "left": "left",                    
"center": "center",                    
"right": "right"                
}           
 },
thumbnail
Fernando Fernandez, modified 3 Years ago.

RE: Access Liferay-JavaScript-Object in React-Portlet

Expert Posts: 396 Join Date: 8/22/07 Recent Posts
We have been using the Liferay javascript object in 7.2 CE React portlets without any difficulty and simply invoking it on any  javascript method/function.



import React from 'react';
import ReactDOM from 'react-dom';

import SearchComponent from './SearchComponent';


/**
 * This is the main entry point of the portlet.
 *
 * See https://tinyurl.com/js-ext-portlet-entry-point for the most recent 
 * information on the signature of this function.
 *
 * @param  {Object} params a hash with values of interest to the portlet
 * @return {void}
 */
export default function main({portletNamespace, contextPath, portletElementId}) {
    Liferay.Service(
        '/cust.customer/get-customers',
        {
          start: -1,
          end: -1
        },
        function(custList) {
          console.log("ajax returned:",custList);
          ReactDOM.render(
            <searchcomponent portletNamespace="{portletNamespace}" contextPath="{contextPath}" portletElementId="{portletElementId}" custList="{custList}" />, 
            document.getElementById(portletElementId)
        );
        }
      );
}
thumbnail
Fredi B, modified 3 Years ago.

RE: Access Liferay-JavaScript-Object in React-Portlet

Junior Member Posts: 69 Join Date: 4/1/20 Recent Posts
Hi Fernando,
are you using this approach with "adapted React-Applications"?
Because I am only able to get this working by creating a Liferay-React-Application and not when adapting a already existing React App. 
thumbnail
Fernando Fernandez, modified 3 Years ago.

RE: Access Liferay-JavaScript-Object in React-Portlet

Expert Posts: 396 Join Date: 8/22/07 Recent Posts
Didn't try those, sorry.