N
N
nathan1117772019-10-05 15:33:54
JavaScript
nathan111777, 2019-10-05 15:33:54

How to export from the App.js file not only the App component but also an array that is outside the App component?

I passed the App component to the index.js file:

import React from "react";
import ReactDOM from 'react-dom';
import App from "./App";

ReactDOM.render(<App attributeApp={carsArray} />, document.getElementById('root'));

But in addition to the App component, I also have the carsArray array in the App.js file :
import React from "react";
import ReactDOM from 'react-dom';
import {BrowserRouter, Route, Link, Switch} from "react-router-dom";
import createBrowserHistory from "history/createBrowserHistory";
import './App.css';
import Header from "./smallComponents/Header.js";
import Footer from "./smallComponents/Footer.js";
import Home from "./pageComponents/Home.js";
import About from "./pageComponents/About.js";
import Contacts from "./pageComponents/Contacts.js";
import CarsList from "./pageComponents/CarsList.js";

let carsArray = [
{
    id:1,
    name: 'BMW',
    phone: '555 555 5555'
}, 
{
    id:2,
    name: 'Mercedes',
    phone: '555 111 5555'
},
{
    id:3,
    name: 'Audi',
    phone: '555 777 5555'
},
{
    id:4,
    name: 'Ford',
    phone: '555 888 5555'
}
]

const history = createBrowserHistory();

class App extends React.Component {
  render(){ 
    return (
      <BrowserRouter history={history}> 
         <div className="wrapper">
        
        <Header/>
        
       <CarsList attributeCarsList={this.props.attributeApp}/>
        
            <Switch>
              <Route exact path="/" component={Home} /> 
              <Route path="/about" component={About} />
              <Route path="/contacts" component={Contacts} />
            </Switch>
    
        <Footer/>          
        
       </div>
    </BrowserRouter>
   )
  }
}

export default App;

If the array is not passed to index.js, then there will be an error.
what to write after:
export default.....
to pass both the component and the array to index.js?
and how to write array import in index.js?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Suha, 2019-10-05
@nathan111777

export let carsArray ...
import App, { carsArray } from "./App";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question