Random quote machine project react

i want to use function to get a random quote from the api but this function failed to import a quote and this is my code please help me to fix it :

function getQuote() {
   return fetch('https://type.fit/api/quotes').then(response=> {
   return response.json()
  }).then(jsonResponse=> {
    if(!jsonResponse.length) {
     return []
    }
    return jsonResponse[Math.floor(Math.random() * jsonResponse.length)];
  })
}
export  const textQuote = getQuote().text;
export  const authorQuote = getQuote().author;

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ā€™.

https://www.freecodecamp.org/learn/front-end-libraries/front-end-libraries-projects/build-a-random-quote-machine

I have some questions about your implementation.

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.

Also, this:

export  const textQuote = getQuote().text;
export  const authorQuote = getQuote().author;

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?

ok my friend thatā€™s my all files project :
this is the text.js file :

import React from "react";
import './Text.css';

class Text extends React.Component {
   render(){
   	return (
   		<div id="text" >
   		<h2>This is your quote text</h2>
   		<p>{this.props.text}</p>
   		</div>
   	)
   }
}

export default Text; 

this is the App.js file :

import './App.css';
import Text from '../Text/Text';
import {textQuote, authorQuote} from '../../RandomQuote/RandomQuote';


class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      text: textQuote,
      author: authorQuote
    }
  }
  render() {
    return (
        <div id="quote-box" >
        <h1> My quote</h1>
          <Text text={this.state.text} />
        {/* <Author />*/}
      {/*<Button />*/}
      <a href="https://twitter.com/intent/tweet">Tweet</a>
        </div>
    )
  }
}

export default App;

this is the index.js file :

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();

import {textQuote, authorQuote} from '../../RandomQuote/RandomQuote';

Wait, what? Where is your RandomQuote folder relative to your App folder? Is is a sibling of ā€œcomponentsā€?

Can you export anything from that file? Maybe comment out the other stuff and just try:

export const foo = 'bar';

and see if you can import that.

Mr Kevin this is

It doesnā€™t look like you gave RandomQuote a file time. I think import assumes ā€œ.jsā€.

2 Likes

RandomQuote folder is in the global folder ā€œsrcā€ and ā€œapp.jsā€ is within the sub folder of the src in the ā€œComponents/Appā€ folder

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.

1 Like

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 ?

ā€œwrongā€ is too strong of a weird.

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.

1 Like

In your opinion i must create my randomQuote in the components folder

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?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.