Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
React Global Components
How can I reuse a React Component in separate React portlets?
for more detail, assume I have two react portlet and they import the same Component named Footer,
How and Where can I define Footer that can be used in both?
Each portlet, by definition, should be able to render itself regardless of the page that it gets dropped on. There should be no dependencies on other portlets or even the global context because there is always a possibility those things won't be there.
A portlet is still just a portlet, even if it is written in React. Give them both the same footer component and let them render it.
That said, you're free to use your own NPM repository, build React components and publish them into your NPM repo, then each React portlet could depend upon that component from your repo, but that's compile-time management of your components, not runtime management.
Liferay has put a lot of effort in their loader to support things like that. You should be able to export your component in one bundle and require/import it in another module (or portlet). I have never tried it, but it should be possible.
https://portal.liferay.dev/docs/7-1/tutorials/-/knowledge_base/t/referencing-an-npm-modules-package
https://portal.liferay.dev/docs/7-1/tutorials/-/knowledge_base/t/obtaining-dependency-npm-package-descriptors
And as David said the best solution is creating npm package and importing it into various portlet.
https://github.com/izaera/liferay-js-toolkit-showcase/tree/react
That may inspire you on how to structure your solution.
"config": {
"imports": {
"react-provider": {
"prop-types": "15.6.2",
"react": "16.8.6",
"react-dom": "16.8.6"
},
"common-component": {
"/": "1.0.0"
},
"redux-store": {
"/": "1.0.0"
}
}
}my common-component module contains one Button.js component and I am trying to use this Button component into test-react-portlet as import Button from 'common-component' but I am not able to do this. I am getting below error on browser console while going to use test-react-portlet in the page.Liferay AMD Loader: Unhandled failure : <script src="http://localhost:8080common-component@1.0.0/src/index.js?languageId=en_US"> while resolving modules.see the src url inside script http://localhost:8080 missing slash(/o/resolved-module/) , I think it should be http://localhost:8080/o/js/resolved-module/common-component@1.0.0/src/index.js?languageId=en_US.Now what I have to do.
To reuse a React component like Footer
in multiple React
portlets, you can define the Footer
component in a shared
or common directory that is accessible to both portlets. Typically,
you would create a separate folder, such as
src/components
, and place the Footer.js
component there. Both portlets can then import the Footer
component from this centralized location. For instance, in both
portlet files, you would import the Footer
component
using a relative path like import Footer from
'../components/Footer'
. This allows both portlets to use the
same Footer
component without duplicating code.
Additionally, if your application is large or uses micro-frontends,
you might consider a package manager or module bundler that handles
dependencies across different portlets, ensuring the component remains
consistent across them.
Powered by Liferay™