Can I have two separate files? React.js and HTML file - javascript

I am learning React.js, and I have noticed that each React file appears to be just a mix of JavaScript and HTML. But I enjoy having distinct files for my HTML and JS. So, I am wondering if I can have these two independent files but also include a link (or something) in either the HTML or JS file, so that they may communicate with one another.
Thank you.

React components implement a render() method that takes input data and returns what to display. This example uses an XML-like syntax called JSX. Input data that is passed into the component can be accessed by render() via this.props.
In other words, it was more simple than we separate html and render it on React, as a component.
If we still want to access html page via any page that created with react, we can did it via a href.
For any other way like import it (maybe), i think it was too complicated because of some reason.
Hope you will more understand about react with this react docs link

React is used to build single-page applications which is an application that loads a single HTML page and all the necessary assets (such as JavaScript and CSS) required for the application to run . this single HTML page is index.html located in the public folder (if you're using create-react-app)

Related

How use simple VueJS component in Laravel project with VaniiliaJS

I have a Laravel (9) project but I don't use Inertia (or anything else). I use just VanilliaJS and webpack (mix) and render it with "simple" blade files.
BUT I want to use VueJS (3) only when I need it in some pages (as a component form that I want to reuse in several places).
As VueJS uses a virtual DOM and the component is rendered only on one page, the vanilliaJS that I use (also) on this page does not work anymore as it is bind on the "real" DOM.
So my question is how to use vuejs as a simple component and still use VanilliaJS on the same blade page?

Is it possible to embed a dynamic react component into a separate html file without downloading a file or using a server?

I am using React to make a SPA where you can customize a component (font size, color, etc) and then embed the customized component into a website. Is there a way to bundle the component and express it as HTML where the user can simply copy and paste it into their website?
I have tried using iframes, but it is fetching data from a URL. I'd like to avoid this if possible since the module/component/widget needs to be copy/pasted.
I have also thought about using webpack to bundle the component using the UMD library target, but I can't find a way to bundle the file in realtime- where it includes the user's customizations.
Lastly, I have thought about rendering a html string which is about as close as I have got to what I want, but it does not include any styles.
This is what the component I am trying to make universally modular looks like:
<LinearLayoutDiv>
<ModuleHeader />
<ModuleStoryDiv>
{[...Array(numStories)].map((val, index) => {
const data = generateFakeArticle();
return <ModuleStory
key={`module-story-${orientation}-${index}`}
picture={data.picture}
summary={data.summary}
distance={data.distance}
author={data.author}
orientation={orientation} />
})}
</ModuleStoryDiv>
<ModuleFooter />
</LinearLayoutDiv>
I am using styled components and a theme provider to pass user customizations down to the component.
How should I go about doing this? Any advice would be greatly appreciated.
How I ended up doing it was like so:
There are two parts I need to create a fully modular widget: HTML and CSS.
I am using styled components. So to get the CSS, I used their secret internal stylesheet to get all the styles of my application. This part is inefficient since I have no way of telling which CSS belongs to the module and which CSS is part of my application so it just takes all of the CSS. Now I have a CSS stylesheet that can be embedded in a style tag.
import { __DO_NOT_USE_OR_YOU_WILL_BE_HAUNTED_BY_SPOOKY_GHOSTS as scSecret } from 'styled components';
const CSS = scSecret.StyleSheet.instance.tags.map(tag => tag.css()).join('')
To get the HTML I made the top most component of the module into a secondary root, and used .outerHTML to get the HTML of itself and its children.
document.getElementById('secondary-root').outerHTML
With both the HTML and CSS I can create a simple HTML doc that can be copy pasted into a separate website.

How do I access a component's HTML outside an Angular v6 app?

I have created a component outside app using the Angular CLI (in src folder)
../src> ng g c test
And added the component import in app-modules.ts
I'm not able to access the test.html file separately (I want to execute the code in the ngOnInit method of test.ts) as below
http://localhost:4200/<<context_path>>/test.html
I even created HTML and a JavaScript file and tried to access, however that didn't work.
Kindly let me know is it possible to access the HTML?
Thanks for the response. Below the screenshot
Application screenshot
i want to access like http://localhost:4200/test.html
this called bad approach you shouldn't create components out of src folder i don't mean by that is not gonna work but not recommended to do that src folder excite to gather all your project component to make it easy to access and sharing the data between them.
alternative solution 1: you can delete the component you made out of src folder and recreate another one in app folder or simply move the component files/folder in the app that's the correct approach.
then you can access to it from any other component in your app by import it in ts file
alternative solution 2: if you want just to sharing data or executing method/function that maybe shared by two components that holds some data you can use services or event binding.
alternative solution 3: if your problem with routes and you want to naviagte to this test.html for recommended approach firstly move it inside the app folder like alternative solution 1 then use routes and it's config to create a route for this component.
notice: you couldn't inject only html file inside angular components it must has his own ts file that holds the component config #Component({...}) that's how angular knows that's component excite and how to inject it to other components.

Can Knockout components be registered by a dynamically generated <script>?

The motive is that I don't want to register all my components when the website is first visited by the browser. I want to be able to register components when a code needs it, and I don't want the JS file of the component to be loaded when the web page loads the very first time. Via Ajax, I want to load JS files containing component registration code.
Please, is this possible in Knockout?
If you want to load components in a different way than provided by Knockout's default loader (which uses ko.components.register), you should create a custom loader (docs). It seems that in your case, you'll just want to implement the getConfig method to return the configuration for a component.

vuejs include javascript library in spa

i'am creating spa application using vuejs and i find out that i have 3 option in loading my javascript library like bootstrap.js or jquery.js and other javascript library:
1.
first is by include all javascript library that i will use in my application in index.html where my vuejs application will live but i find that there is some javascript library that not working to well
ex: there is some javascript library that calculate page height by selecting some div with specific id="page-container", but that div not loaded when page is rendered from server, so at that moment the javascript will throw error since id="page-container" not exist yet.
2.
second is by adding it like this to all my javascript library js
// before you use your files in some components,you should package them
// your local files
export default { //export your file
your_function(){ // defined your function
...
}
}
// now your can use it
// your component file
<script>
import local_file from 'your_file_relative_path'
//now you can use it in the hook function
created(){ //or other hook function
local_file.your_function() //call your function
}
</script>
but that mean i need to change every javascript library that i use...
3.
third is by adding it using npm, and just in the vue component import it, it works okay and feels more natural but not all my javascript library are in npm, some of them is admin template related that i bought from themeforest and will never be in npm.
so which one is a better way or maybe there is much more better way that those 3 option that i find out? its hard to find any tutorial or discussion that mention adding other javascript library to spa vuejs most of them just put a bootstrap into index.html and done.
Well, If your library exist in NPM, then this is the best option, because then you have this option to import only the part of the script that you need for certain components, for example, fontawesome library, you can import only the icons that you need instead of import all of them!
but if your script is not in NPM, the best option is to run your script in beforeMount or beforeCreate of the component that the script needed to run.
the third way which is add the link reference on html is not really suggested, since it will be global and will reduce the performance.

Categories