I have a Meteor app built using blaze. Now I am shifting the UI to react. I have just started learning react and hence I am confused how to use #with, #each, etc., in reactjs.
Code Sample:
<div className="page-content {{#unless}} FLT rd-body {{/unless}}">
How to use '#unless', '#with' and other Meteor components in ReactJS?
Also, how to use Session variables in reactjs?
React is a library to create web-ui e.g. the 'view-layer'. Meteor is a framework, build-system and server all-in-one.
Blaze is the default view-layer. In Blaze you can make your html dynamic with the {{ .. }} tags, as you use above.
React works differently. React templates are Javascript files mixed with HTML. Usually with extension .jsx. I'd visit https://reactjs.org/ for more information, or one of the many tutorials and books that exist.
Read the manuals:
https://guide.meteor.com/react.html
and
https://www.meteor.com/tutorials/react/creating-an-app.
Especially the first link.
It contains the syntax to embed react JSX into blaze templates or use blaze templates as JSX components. So you basically have to decide which templating system you're going to cast to the other system.
Concerning the session variables, have a look at the react documentation.
Related
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?
I have an existing application build with .Net Core Framework. I would like to integrate React components for re-usability purposes which at this point will only be app specific. I have gone through numerous "Hello World!!" tutorials but that doesn't satisfy my need. I have also looked at reactjs.net but that also is not going to help me as the components gets rendered on the View
Scenario
Application has lots of Modals with a form which gets rendered on numerous pages. Currently it is being handled with JavaScript. The JavaScript code gets duplicated a lot to achieve it.
Goal
Would like to have a react component to replace above mentioned functionality to reduce code getting duplicated.
The problem I am facing is I am not sure how will I be able to interact with the component from a jQuery/JavaScript point of view.
Example
I have a DataTable and one of the actions is to click on a certain button to display the Modal. The code is in a separate .js file so it is separate from the View. So in this case if I click on a button I would like to render the react component. I would need to pass props through to the components and that is where I am uncertain how would I handle it :-(
Any suggestion or guidance would be appreciated.
Thanks in advance
Using react in Asp.Net Core application is easy. At first you need to know your back-end will be API based to communicate with react. So if you have not set up your Core application to an API based, it won't work. Also make sure you have installed nodeJs and other dependencies.
To get started with react, create a new folder in your Asp.Net project solution.
Open the folder in your Command line and execute:
npx create-react-app [your--folder--name]
To view your created app, run
npm start
To get started with asp.net, you need to add spa dependencies in your project.
Then you have to set up your ConfigureService method to include:
services.AddSpaStaticFiles(configuration => { configuration.RootPath = "[your--folder--name]/build"; });
Finally set up your Configure method to include:
app.UseSpaStaticFiles();
app.UseRouting();
app.UseEndpoints( ... );
app.UseSpa(spa => {
spa.Options.SourcePath = "[your--folder--name]";
if (env.IsDevelopment()) {
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
I'm building a project in Vue.js, but it has a quite a lot of Javascript processing (it's all local, so I don't need to have anything server side), and I was wondering if there is a way to import a file that is basically just some processing (mathematical, structure processing, etc.) functions in Javascript - no template, etc. - into a Vue component? It's all working fine using methods in the component, but getting a bit large and "monolithic".
You can keep working with several files and then just to the following.
import AlgebraicFunctions from 'algebraic-file';
import GeometricFunctions from 'geometric-file';
Using the spread operator:
methods: {
...AlgebraicFunctions,
...GeometricFunctions
}
I am making custom templates and components on the client side. I need to include a javascript library that only shows up when the component/or template is present, and only once. Is there a good way to make sure it is available when needed and disappears when not?
is your js library packed as npm module?
Are you using react or blaze?
There is template_rendered_code which executes each time component is rendered (you need this one if lib does something with dom).
Most likelly onCreated and onDestroyed (Blaze) or componendWillMount and componendWillUnmount (React) us what you need, but I didn't implemented this in the kitchen (I added this to my Trello tasks).
I am working on an isomorphic javascript app with express + react. We started out using jade for server side templates for static content, but combining the two is quickly becoming unwieldy. We have ended up with something like this:
In the express routes:
router.get("/", function(req, res) {
var webpackStats = require('../../config/webpack-stats.json');
var reactHtml = React.renderToString(HiwApp({}));
var slideshowHtml = React.renderToString(slideshowApp({}));
var config = {
webpackStats: webpackStats,
reactOutput: reactHtml,
slideshowHtml: slideshowHtml
};
res.render("how_it_works/howitworks", config);
});
In Jade:
body
.company-logo.center
#react-main-mount
!= reactOutput
include ./content_block_1.jade
include ./content_block_2.jade
#slideshow-main-mount
!= slideshowHtml
This is very brittle-if we want jsx then a jade template then more jsx, we have to make sure we get the order right.
My idea is to do it all with jsx. I know there is React.renderToStaticMarkup for this sort of thing, but that doesn't solve the problem of mixing dynamic with static pages.
The big questions: if we decide to do all of this with jsx (say layout.jsx which contains all components), then call React.renderToString(App({});, will this be a major performance hit? If so, is there a better way to do it to easily combine static and dynamic blocks?
Although this may be a tiny bit off topic: We stuck with jade templates.
Basically we wanted the flexibility to use a non-react + flux architecture for areas of the site when and if that need arose. Our site is basically made up of a number of smaller SP apps: Site, UserAccount, Team and Admin.
Why did we do this?
Smaller filesize and overhead for users who are not accessing all sections of the site.
Option to "opt out" of React and flux if and when the need arises.
Simpler, server side authentication.
The way we have done it successfully was to render a JSX shell template (Html.jsx) on the server using React.renderToStaticMarkup() and then send it as the response to every server-side express route request that is meant to deliver some HTML to the browser. Html.jsx is just a shell containing html head information and GA scripts etc. It should contain no layout.
// Html.jsx
render(){
return (
<html>
<head>
// etc.
</head>
<body>
<div
id="app"
dangerouslySetInnerHTML={{__html: this.props.markup}}>
</div>
</body>
<script dangerouslySetInnerHTML={{__html: this.props.state}</script>
<script>
// GA Scripts etc.
</script>
</html>
)
}
Remember it is totally fine and even recommended to use dangerouslySetInnerHTML on the server when hydrating your app.
Dynamic layout should be done with your your isomorphic components through a hierarchy of components based on their state/props configuration. If you happen to be using React Router, then your router will render view handlers based on the routes you provide it so that means you don't need to manage that yourself.
The reason we use this technique is to architecturally separate our "App" which is isomorphic and responds to state from our server-side template shell which is just a delivery mechanism and is effectively boiler plate. We even keep the Html.jsx template amongst all the express components within our app and do not let it mix with the other isomorphic React components.
One of the most helpful resources I found for working out React/isomorphic architecture was https://github.com/yahoo/flux-examples/tree/master/react-router which is where we stole this technique from.
We explored the idea of integrating handlebars as a templating engine for client's devs using our products in the future but decided that it was less complex to write our own DSL in JSX and use some simple parsing routines to parse our HTML-like DSL to JSX by adding things like export default (ES6 module syntax) at the start of the template and then import the template to a rendering component.
You could of course follow that line of thought and use a jade compiler to spit out the template and then add module syntax around that if you think separate jade files are essential. I also noticed this project as well although I have not explored it in anger: https://github.com/jadejs/react-jade.