how can I share components in two projects in NextJs? - javascript

I don't know which is best option to share component ReactJs in my particular case, I have two application in NextJs, one is e-commerce and another is a manager portal for this e-commerce.
In first app (e-commerce) I have UI Components (buttons, fields, headings, texts, etc), and I want to use these components in the other project, I been thinkings uncouple UI Components and create a new repo, and this repo sharing to both project.
I don't know if this is right thing to do that?
Can I create this repo (UI) in NextJs for use benefits of them? with NextJs is very easy to use Typescript, EsLint, also NextJs include base template PostCSS, and with nextjs almost hardly need config anything.
What do you recommend?
Can I use Next Js only to create repository of components? is recommend?

You need to create a component library.
Component library using bit
Create react library
Here's an article with your question as the header: https://blog.bitsrc.io/how-to-share-react-components-between-nextjs-projects-c0857bbc1fcb

Related

How to use a NUXT component in another site

I have a working NUXT application with various pages and components in universal mode. Now I need to render one of the components on another static html site.
I can easily export a regular Vue application's component just adding a bundle script and div element to which the components renders.
But how can I do it with NUXT?
Nuxt is not really meant for a quick plug (with a script tag) but for an SSR usage (with some NodeJS build), so I highly doubt that you can make this. Or at least, I don't really see the point if you only use it as an SPA component.
If somebody knows a solution to make it work, I'm all yours on your opinion on this.

Creating a Javascript bundle with a few components for reuse

I have a backend rendered page (django in case it matters) which I want to soup up a little using some components from PrimeVue and a markdown editor packaged as a vue component. Right now, we have a few small animations using jquery for which we include jquery from a CDN directly into our pages. A few months ago, we needed to spice up a page using some more client side interactivity and we included vue.js via a CDN onto that page (dropping jquery) and then wrote some javascript in an index.js that we also loaded up from a CDN and got our work done. This is the current state of affairs. The page currently looks like this
<html>
....
<script src="https://cdn/vue.js"></script>
<script src="/static/index.js"></script>
The div #mainvue is where vue runs and does what it needs to.
This is where we are now.
Using plain vue is okay. Now, I'd like to throw in a few components from primevue as well as a 3rd party markdown editor that's wrapped as vue component. I want to bundle all of these as wel as plain vue itself into a single javascript bundle that I can throw onto a CDN and include into all my pages. Then my devs can do their day to day work in the index.js.
Is this a reasonable approach and if so, how do I do it? I'm not familiar with the javascript ecosystem. If not, what's the right way to solve this problem. I don't want to go all the way SPA and REST API. I just want to use a few 3rd party components and vue on a simple otherwise backend rendered page.
Since you mention you don't want to "go all the way SPA," a reasonable hybrid to is to use Vue in MPA (multi-page app) mode. This will require using a vue-cli/webpack configuration to compile your Vue components into bundles, but once you have this build pipeline, these bundles can used in individual Django templates via django-webpack-loader. Information can be passed from Django via template variables directly as Vue component properties.
Re bundling, yes you can bundle all these resources into a single JS using this method, but it's nearly as easy (and far more performant) to create one or more common bundles that represent shared logic (third party libs, invidual components, even Vue itself) and then pick and choose from among these bundles as needed on individual Django templates.
The steps to implement are a bit too involved to post directly here, but I've written a series of articles Django + Vue -- Best of Both Frontends that explains. There's also a cookiecutter for boostrapping new projects using this method. I realize you already have a site, but you can perhaps adapt the implementation there.
Good hacking!

multiple react apps (components) on the same page?

I need to develop some basic components to add them to an existing webpage. I want to start with the default create-react-app. In the end, I will end up with src/FirstComponent.js and src/SecondComponent.js
I need to build both components individually. When I run yarn build i get a main.hash.chunk.js that hash the two components merged. Is there anyway to end up with two different main files? Is that a bad approach?
If I need to use a third party library, lets say axios, and I will need them on both components... Will it be imported twice?
Any advice would be appreciated!
If I need to use a third party library, lets say axios, and I will
need them on both components... Will it be imported twice?
If the existing webpage you would like to plug these two new components into is a ReactJS app then you might want to consider making these two components presentational components (for more info). The axios request will happen in the existing web page and the data that each component needs will be available to them via props. I hope that helps somehow.

Is it possible for Vue Web Components to have listeners or interact some other way?

I am trying to develop multiple Vue.js components written as .vue single file components. The requirements is that such components need to be embedded by front-end designers directly into HTML using element.
Since i cannot achieve this with default compilation method provided by webpack, i figured out it could be done by compiling (more correctly, transpiling) as web components.
Is it possible to use Vue Web Components with listeners as you would with regular Vue Components?
<some-web-component #some-event="someFunction()"></some-web-component>
I couldn't find any examples of this, all which i found had only a simple component with some props, displaying a message. Is this even possible?
If not, is there some other way to achieve communication between Web Components?
Similarly asked here:
https://forum.vuejs.org/t/using-vue-single-page-components-directly-in-html/66384
For anyone wondering the same, I've found the solution to be using Vuex store.

How to use react with material ui along with python-django

I need help to use react with material ui components in web application.
The following key technologies i worked on for several projects:
Materialize CSS, Javascript, Jquery.
The following key technologies that i want to work with for new projects:
Material UI, React
Is it good idea to work with react or to have Angular ?
How to start making a project having both django and react with material UI components?
References :
https://medium.com/codingthesmartway-com-blog/getting-started-with-material-ui-for-react-material-design-for-react-364b2688b555
https://medium.com/#TechMagic/reactjs-vs-angular5-vs-vue-js-what-to-choose-in-2018-b91e028fa91d
Front-end and backend are two separated things.
You can use whatever database, because your backend will work with that.
You can use whatever as backend.
React/vue/angular don't care how you will be communicating with server or what backend you use.
You can start with set up frontend development for react using for instance facebook-create-react-app which would set up basic development boilerplate. You will also need to take care of client-side routing (you can use react-router and you will need some way to comunicate with your backend for example with ajax calls to get your data using ES6 fetch() or some js library like axios.
It is up to you how you will handle backend but i would recommend you to use RESTfull API with django which would be communicating with your front-end
I'm doing this on a project right now. The project was set up for me with Docker however, which I'd recommend, it's very well contained. But if you don't want to use Docker you could use create-react-app. I'd make sure this is in a separate directory from your Django project, but in the same git repo.
create-react-app will create a development server for you to develop the front end with.
You'll need a few other libraries in your React code besides Material UI to fill a couple other needs:
client-side routing, I recommend react-router (for use with MUI see this docs page)
communication with the backend Django server, I recommend GraphQL.
Traditionally you'd use a RESTful API but these are very difficult to design well and have limitations in traversability and ability to change over time. I think that GraphQL is the future of web APIs and everyone should check it out.
To implement a GraphQL API in Django you can use graphene-django which integrates well with Django's ORM (models). It's documentation is thin, but I've found it works very well.
On the client side there's a few React libraries for connecting to servers with a GraphQL API. I'm using Relay which is from the same developers as React and GraphQL, but Apollo could also be a good option depending on the state of its development.
Eventually, when you deploy the project you'll need to compile production static files out of create-react-app or Webpack or whatever you're using to compile your React code. Then put the static files in a place that can serve them on your site. If you're using a production web sever like nginx the static files can be served directly without reaching Django.
The following key technologies i worked on for several projects:
Backend : Python-Django (MySQL)
Frontend : Materialize CSS, Javascript, Jquery.
The following key technologies that i want to work with for new projects:
What does work mean? If you can write api's in the backend.Go ahead and write some api's using django framework.
Backend : Python-Django (MySQL)
Frontend : Material UI, React
So can you guys help me out to implement this.
What do u mean by implement? Go ahead and find a nice react + material ui + webpack boilerplate and start the application. Try to Consume
your backend api's through it.
Is it good idea to work with react or to have Angular ?
Long discussion,You would have got this answer if you read the article that you posted
Do i need to switch to other database ?
It depends on your project
Take a look at this https://github.com/owais/django-webpack-loader which is a good strategy for launch react build with django (i use django rest) and you can use an html in jinja template where you inject the react JS code
You can eject or use config override top override the default native webpack configuration present in react JS https://www.npmjs.com/package/config-override
I think you should see Preact, which is react integrated in any MVC templating, with that you can override some places of templates by their ids.
By this solution you can inject material ui code inside your templates form or others has you need.
For the injection you need to use custom element,
https://preactjs.com/guide/v10/web-components/#creating-a-web-component

Categories