Enable component import from parent directories
See original GitHub issueIt seems create-react-app@0.2.0 has problems importing modules from parent directories.
If I add a Hello component to the default generated app everything works as expected:
// src/App.js
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Hello from './Hello';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<Hello name="World"/>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
// src/Hello.js
import React from 'react';
const Hello = ({name}) => <p>Hello {name}!</p>
export default Hello;
But when I move Hello.js
to the parent directory and point the import to it, it doesn’t work anymore:
// src/App.js
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Hello from '../Hello';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<Hello name="World"/>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
);
}
}
export default App;
// Hello.js
import React from 'react';
const Hello = ({name}) => <p>Hello {name}!</p>
export default Hello;
Instead I get the following error:
Error in ./Hello.js
Module parse failed: /private/tmp/test/component-example/Hello.js Unexpected token (3:26)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (3:26)
@ ./src/App.js 19:13-32
I would want to find a solution to use react-scripts in a subfolder (for devcards like component development) of a larger existing project. The react-script under the devcards subfolder would use the components from the main project (parent directory).
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
React import from parent directory - Stack Overflow
Try this ./../Component.js . It works for me. More over if you need access a component from adjacent directory ./../AdjDir/Component.js.
Read more >Python - Import from parent directory - GeeksforGeeks
In this article, we will learn how to Import a module from the parent directory. From Python 3.3, referencing or importing a module...
Read more >How to import files from outside of root directory with React ...
So we're going to see how to directly reference any file outside of root directory.. Consider this example: common - components - Utils.ts,...
Read more >How to Use File Choosers - Oracle Help Center
Given a parent component, creates and returns a new dialog that contains this file chooser, is dependent on the parent's frame, and is...
Read more >importlib — The implementation of import — Python 3.11.1 ...
Two, the components to implement import are exposed in this package, ... To properly import a submodule you will need to import all...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
Please see the README that should be generated in your project:
We should add a warning for that…
It would be nice to be able to customise this if possible. Not sure how easy that would be, but at the moment if you have a couple of apps that want to share some components it’s not possible without creating proper modules for them.
If somebody points me in the right direction, I’m happy to submit a pull request assuming that you decide it’s a worthwhile change to make.