how to use JavaScript with react-native? - javascript

How to use javascript insted of typescript?
when ever i create react native app by using npx react-native init ,i get typescript insted of javascript.
any idea guys?
i could not find anything online

Using JavaScript Instead of TypeScript
React Native defaults new applications to TypeScript, but JavaScript may still be used. Files with a .jsx extension are treated as JavaScript instead of TypeScript, and will not be typechecked. JavaScript modules may still be imported by TypeScript modules, along with the reverse.

Using expo and npx create-expo-appcommand let me to create a react native app with js. For more info click here

Related

Can I use TS classes/interfaces in a simple JS mean application?

Let's say that I have a build up MEAN application written in JS, and I want to keep it but add some classes/interfaces using TS (I want to do this because I want interfaces) and use it in my controllers to communicate with my backend. Is it possible? If yes, how do i do it?
Thanks in advance!
Since TypeScript is a superset of javascript all JS code will work in typescript BUT you need to change the file extensions from .js to .ts
Furthermore, all typescript code must be compiled back to javascript before run. which means a typescript compiler has to be installed and configured.
That was my answer to your question.
As a piece of advice since you are building with MEAN stack then you are used to Angular way of structuring code.
If that is the case then you can build your server the same way with TypeScript using a framework called NestJS which is very very similar to Angular:
https://nestjs.com

How to translate a tsx file to a js file for React

I am working on a React Project and have a bunch of tsx files that I want to convert as use as javascript for my project. How should I do that?
I am working on a React Project and have a bunch of tsx files that I want to convert as use as javascript for my project. How should I do that
You can use the typescript compiler e.g.
npx -p typescript tsc somefile.tsx
Ciao, while the web is full of guide on how to translate React Javascript into Typescript, I don't found any reverse guide (so strange...).
Anyway, I had same problem some time ago and I found useful get one of those guide JS => TS for React and read backwards. For example, I used this guide. When he said "JavaScript looks like that so you can translate this into Typescript in this way..." I did the contrary (and it worked!). Of course you can skip all the dependencies part.
Try and let us know.

Exporting a dynamically created function for use in a module

I'm relatively new to modern Javascript dependency management. I'm writing a web app in Typescript and have started to use Jasmine as a unit test framework.
Once I started using Jasmine it became clear I needed to start exporting/importing the classes and functions in my source code files, since there is no HTML page loading all of them via script tags. This has been fine for my own files, but I am using a third-party library that is provided minified:
https://currency.js.org/
https://unpkg.com/currency.js#1.2.2/dist/currency.min.js
I can't seem to get my code running via Jasmine to recognize the existence of the currency function defined in this file. I'm guessing it's because the function seems to be dynamically created.
What would be a proper way to export the currency function above for use in my own modules?
That package actually is published on npm (with TypeScript typings included), and I was able to successfully import it as a module in a test application:
npm install currency.js
Then in your code:
import currency from "currency.js"
console.log(currency(1.23).add(.01).format());
https://www.npmjs.com/package/currency.js

is it possible to use a NPM (a module has made by angular 8 and converted to NPM) in javascript or even react native?

I've made an Angular (v8) module and want to convert it to NPM to use it as a library (package) in a normal JavaScript app (or even in react or react native app).
Is this possible?
No, right out of the box, React and Angular are very different frameworks. They don't just interop with each other.

Can I use reactJS library in react-native?

Well, this might be a silly question but I want to clarify the reason.
React-Native imports nodeJS libraries, so I think it is possible to use reactJS library as well though reactJS includes pure html components.
Can react native recognize reactJS components including html?
react library actually does not have anything related to Browser DOM HTML. Anything related to it separated into react-dom package. React Native does not and cannot use this library, because you don't have DOM underneath a react native application. However you can use most of the code/functionality you wrote for your mobile app in the browser, if you install necessary transpiling library. This is possible because react native defines some primitive components that can be ported to almost any platform.
If you still want to use just HTML to render inside react native, you may use WebView for it.
Usually libraries built specifically for other platforms will not work with React Native. Examples include react-select which is built for the web and specifically targets react-dom, and rimraf which is built for Node.js and interacts with your computer file system. Other libraries like lodash use only JavaScript language features and work in any environment. You will gain a sense for this over time, but until then the easiest way to find out is to try it yourself. You can remove packages using npm uninstall if it turns out that it does not work in React Native.
-- source: React native official docs

Categories