0

I have the following in App.js

let contracts = [
 {
   id: "1",
   name: "Lloyds",
   image: ""
 }
];

class App extends Component {
 render() {
   return (
     <div>
       <Header />
       <Search />
       <ContractsList />
       <Content />
       <Footer />
     </div>
   );
 }
}

export default App;

Im trying to pass the contracts variable as a prop in my index.js file

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import "bootstrap/dist/css/bootstrap-grid.css";

ReactDOM.render(
 <React.StrictMode>
   <App contracts={contracts} />
 </React.StrictMode>,
 document.getElementById("root")
);


but keep getting the following error:

 Line 10:21:  'contracts' is not defined  no-undef

how can i use the contracts variable so it can be used as a prop in other components?

5 Answers 5

1

You need to shift your contracts array from app.js to index.js and then pass it as a props.

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
import "bootstrap/dist/css/bootstrap-grid.css";

let contracts = [
 {
   id: "1",
   name: "Lloyds",
   image: ""
 }
];

ReactDOM.render(
 <React.StrictMode>
   <App contracts={contracts} />
 </React.StrictMode>,
 document.getElementById("root")
);
Sign up to request clarification or add additional context in comments.

Comments

0

contracts is not defined in your index.js file. Either you define contracts there, or you import it.

And then, you need to pass it to the underlying components in App.js too, for example:

<Content contracts={this.props.contracts} />

(For every component in which you need it.) Why are you trying to pass it in index.js at all?

Comments

0

The error is coming on the index.js file on this line:

 <App contracts={contracts} />

here you dont have any variable called contracts, you may need to put a variable contracts in this file instead of App.js.

Hope this helps!

Comments

0

You've defined contracts in a different file to the one you're trying to use it in. Define contracts in index.js and it'll work.

Comments

0

you need to export the contracts

Demo

App.js

export const contracts = [
 {
   id: "1",
   name: "Lloyds",
   image: ""
 }
];

index.js

import App,{contracts} from "./App";

For better. its already in app.js . so you can access with in app.js .no need to pass from index.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.