and when i export and import the textQuote and authorQuote variables from the randomQuote.js file to app.js file thereās an error appear :
./src/Components/App/App.js
Attempted import error: āauthorQuoteā is not exported from āā¦/ā¦/RandomQuote/RandomQuoteā.
First of all, does your function getQuote return an object or an array? This (return []) returns and array and this (return jsonResponse[Math...) returns an object.
is going to get a text from one random entry and get an author for a different random entry. You are generating a different random number for each.
Also, I donāt think exporting those two values is going to get the result you want. That is going to calculate each of those at the beginning at export those. Furthermore, as near as I can tell, since there is no await or Promise handling, those are just going to evaluate to a Promise?
What I would expect is for the getQuote function to be exported and consumed where it is needed (and only called once for each quote). I would expect that Promise to be handled, either by handling it directly or dealing with it with an async/await.
As to why it is not importing/exporting properly, I donāt see anything that jumps out at me as wrong. Iād have to see your setup to tell. Do you have a repo for this?
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './Components/App/App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
OK. Thatās a little odd to me. I would expect that to either be in a āpagesā folder (or something like that - I see āscreensā sometimes) or in the ācomponentsā folder (not usually capitalized). Since your app is in the ācomponentsā folder I would think that conceptually you are putting all your components in there.
This isnāt carved in stone, itās just what Iām used to seeing. It also shouldnāt cause this problem.
yes all my components folder is in the āComponentsā folder dividing in two files js file and css file.
so i canāt make this getQuote function in seperate file?
itās wrong ?
And I was a little confused - RandomQuote is not a component so it doesnāt belong there. Really, this is just a helper function. I might create a folder like āsrc/helpersā or āsrc/servicesā. In that case, the file name should probably be camelCase. If you were to turn it into a full module, I might create a module folder and PascalCase it, but I donāt think this needs to go that far.
And donāt worry about this too much. Iām just explaining why it looked odd to me. The casing on the filename made me think it was a React component.
WIth this size of app, these type of things donāt matter much. Itās not until you get into bigger apps that it will really matter, and you can figure out those issues when you get there. For now, just figure out a way to organize that keeps things neat and maintainable. My only caveat would be to be careful what you put in the src folder - only things that are universal to your all should go there. You basically have that - although I probably would have left index.js there. It just looks odd to me to have the RandomQuote folder there - what if you need 100 helper modules, will they all go there?
But in any case, donāt worry about it too much. I now kind of regret bringing it up - I donāt want you bike shedding about it.
No, that was when I was thinking that it was a component. It is one. Where to put it? Again, there is some subjectivity here, and this probably not something you need to worry about write nowā¦
If you are only using it in App.js, then that is a valid place to put it. If I felt I needed to break it out into another file, I might make a file like src/components/App/App.helpers.js or something like that. I might also create a file like src/utils/quotes or src/services/quotes.
But again, I wouldnāt worry about this too much. Your PascalCasing confused me and I still think putting that folder directly on the src level is a mistake, but itās not a terrible one considering how small this app is going to be. This is the kind of thing that people almost learn better by screwing it up and realizing the importance of organizational hierarchies. You are not there yet.
Weāve already spent way to much time talking about this. Donāt worry about it.
Did you get the initial issues you were having to work?