简体   繁体   中英

How to import component correctly in React.js?

I have this project structure

在此处输入图片说明

Sorry for question, I am new in React, I can't uderstand how I should call this code in ol-map.js

import React from "react";

import {
  Map,
  layer,
  Layers
} from "react-openlayers";

class olMap extends React.Component {
  render() {
    return (
      <div className="Map">
        <Map view={{ center: [0, 0], zoom: 2 }}>
          <Layers>
            <layer.Tile />
          </Layers>
        </Map>
      </div>
    );
  }
}

export default olMap;

in my Map.js page?

import React, {Component} from 'react';
import olMap from '../components/map/ol-test3/ol-map';

class Map extends Component {
    render() {
        return (
          <olMap/>
        );
    }
}

export default Map;

PS This is index.js file and I have no idea why I need it.

import React from "react";
import ReactDOM from "react-dom";
import olMap from "./ol-map";

const rootElement = document.getElementById("root");
ReactDOM.render(
  <React.StrictMode>
    <olMap />
  </React.StrictMode>,
  rootElement
);

In your component .js, make sure you have

import React, { Component } from "react";
class OLMap extends Component {

And then

import OLMap from "./OLMap";

And of course check your folder structure

import OLMap from "../map/ol-test3/ol-map"

名称在默认导出中无关紧要,因此您可以在导入时命名任何您想要的名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM