score:23
let's say the file you're keeping your data in is called "file.js."
var text = "hello world!"; //in file.js
below this line, you must export this variable so that it can be imported in the react code:
module.exports = {data: text} //in file.js
the string is stored as an object called "data." you then import the contents of "file.js" in the file you wish to use it, let's say app.jsx".
import file from './file.js'; //in app.jsx
the contents of the object you exported in the other file is being stored as the variable "file" here. you can then convert the contents of "file" to an object:
var str = file.data; //"data" is the object within "file"
your string, "hello world!", is now stored within the variable str.
hope this helps!
score:27
you need to export jobs from jobs.js in order to import it into another module. and jobs.js doesn't need to be a function if your just exporting static data. so, you can make jobs.js look like this:
export default {
jobs: [
{
jobtype: "web developer",
desc: "creates website"
},
{
jobtype: "bin man",
desc: "collects bins"
}
]
};
then you can import it like so:
import jobs from './data/jobs.js';
or:
var jobs = require('./data/jobs.js').default;
if you want to require with normal commonjs syntax then you can make jobs.js like this:
module.exports = {
jobs: [
{
jobtype: "web developer",
desc: "creates website"
},
{
jobtype: "bin man",
desc: "collects bins"
}
]
};
which allows you to require like so:
var jobs = require('./data/jobs.js');
Source: stackoverflow.com
Related Query
- How import object from external JS file in React JS using web pack
- How to parse a JSON object from an external file in React using .map() in ES6 and a loop?
- How to get nested Object from JSON File using React JS
- How to import the object from React file to plain vanila javascript file
- How do you import Javascript file from node_modules into react using Vite?
- how to make api call from external file using axios and callbacks and pass param in react
- How to manage dynamic variables from external config file using Electron and React
- using css modules, how can I import classes from a file
- CDN links for React packages and how to import it when using react using the scripts from CDN
- how to import data in react from a js file to use in components
- How to properly import React JSX from separate file in Typescript 1.6
- How to delete object from array using object property - React
- How to import everything from a module in react using es6?
- How to import an external file from project root with webpack?
- How to import a component or file in React using variables?
- How do I access data returned from an axios get to display on a web page using react js?
- How to extract data to React state from CSV file using Papa Parse?
- react js how to import only the required function from a file and not the all functions
- How to import file from public folder in react application?
- Cannot import React JSX from separate file while using Typescript
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- How to import react modules from an HTML file with no npm/webpack
- React - how to populate one select menu based on a value of another select menu using data from a local .json file
- How to call a function in react js from an external JS File
- How to make a list from json object using React
- How to import file from outside /components in react
- how do import the Skype object using react js?
- How to pass data from blade.php file to a react component using HTML element attributes?
- How to import 2,3 object from another file in reactJS
- How To Send Array of Object using FormData() From React Js To Nodejs, MongoDB?
More Query from same tag
- Rendering while cycling through object isn't working
- Multiple react micro front ends on one page
- How to compare typescript type?
- getting custom attribute values
- Redux: yield inside an anonymous function
- How to remove only those commas which are present inside double quotes in a string?
- Laravel 5.5 React
- How to call a query only on refetch
- react-router-dom changes only url
- useEffect changing the desired variable but no effect React
- "GAPI is not defined" message
- can not put file in redux array
- How to use useEffect hook to post a request to postgresql
- Attempted import error: 'unstable_useId' is not exported from '@material-ui/core/utils' (imported as 'useId')
- curly braces and rendering list items
- reactjs onchangevent and keypress event
- Can't write higher order components in Typescript: JSX Element type does not have any construct or call signatures
- FontAwesome change default prefix
- Get Error 'Cannot read property 'setState' of undefined' on setState line
- How can I select all the classNames that ends with a given word in MUI sx property?
- How to update the value coming from web socket server in React.js?
- Mobx makeAutoObservable not binding this
- What are some tips on refactoring lifecycle component methods into useEffect hook?
- Can't access this.state in other function
- How can I pass props from a Parent to Child with this.props.children?
- Updating variable of state and calling in react state?
- Django filter from multiple fields with empty input
- Is there a way to map over an array of objects in react and also get the accosiated images from cloudinary
- Relative Interface (TYPESCRIPT)
- Simple button component not accepting variables