i am learning react. but my basic sample is not working.
my app.js file is like below
import React, { Component} from 'react';
import './App.css';
import { Greet } from './components/Greet';
class App extends Component {
render() {
return <div className="App">
<Greet></Greet>
</div>
}
}
export default App;
my component file is like below
import React from 'react';
function Greet(){
return <h1>Test</h1>
}
when i load my application, error said Attempted import error: 'Greet' is not exported from './components/Greet'. What is this error. how i fix ?
You didn't export Greet function, so you couldn't import it in the App.tsx
import React from 'react';
export function Greet(){
return <h1>Test</h1>
}
import React from 'react';
export function Greet(){
return <h1>Test</h1>
}
You forgot to export the Greet component.
Just add export before function Greet():
import React from 'react';
export function Greet(){
return <h1>Test</h1>
};
Related
I have the following error:
src/Components/navbar.jsx
import React, { Component } from "react-dom";
class NavbarComponent extends Component {
state = {};
render() {
return (
<div>
<nav>This is nav</nav>
</div>
);
}
}
export default NavbarComponent;
src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import * as serviceWorker from './serviceWorker';
import NavbarComponent from './components/navbar';
ReactDOM.render(<NavbarComponent />, document.getElementById("root"));
serviceWorker.unregister();
This is showing up error to me. Can somebody please tell me how to resolve this?
You are importing the component from react-dom instead you should do it from react instead.
import React, { Component } from "react";
I am using d3 v5 with reactJS. I am calling d3.csv inside react 'List' class like following:
import React from 'react';
import * as d3 from 'd3';
class List extends React.Component{
componentDidMount(){
d3.csv("./data/data.csv").then(function(d, error){
console.log(d);
});
}
render(){
return(
<div> </div>
);
}
}
export default List;
and List is being imported in following 'App' class
import React, { Component } from 'react';
import logo from './logo.svg';
import List from './components/list/List';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<List/>
</div>
);
}
}
export default App;
Following is the 'index.js' code:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
'root' is a div id in index.html. By calling console.log in List file inside d3.csv I am getting content of index.html file in the console. But I want CSV content.
You could try using the import statement to load the CSV file at first. :)
import React from 'react';
import * as d3 from 'd3';
import data from "./data/data.csv";
class List extends React.Component{
componentDidMount(){
d3.csv(data).then(function(d, error){
if (error) {
console.log(error);
} else {
console.log(d);
};
});
}
render(){
return(
<div> </div>
);
}
}
export default List;
I had the same issue and here's how I solved it. Check if the data is in the same directory as the index.html file. In my case, my index.html file is in the public/ folder, so I created public/data/data.csv. You can then try d3.csv('./data/data', function(data){...}) to read in the data.
If you are using hooks and useState, ensure that the URL is a string and not resolved as an object before using the data.
Before Input
Before Output
After Input
After Output
I am trying to learn to use react native and am following along with this YouTube tutorial. I have encountered an error stating the following, "Unable to resolve the module ... from ...: could not resolve ... as a file nor folder." I am fairly certain that the file path used is correct and I have followed the video very closely, and it appears to work in this video. Any help with this would be greatly appreciated as I am unfamiliar with using components in react.
index.js
import React, {Component} from 'react';
import { AppRegistry, Text, View } from 'react-native';
import App from './App';
import Component1 from './app/components/Component1/Component1';
export default class myapp extends Component {
render() {
return(
<View>
<Component1 />
</View>
);
}
constructor() {
super();
}
}
AppRegistry.registerComponent('myapp', () => myapp);
component1.js
import React, {Component} from 'react';
import { AppRegistry, Text, View } from 'react-native';
import App from './App';
export default class Component1 extends Component {
render() {
return(
<View>
<Text>This is Component 1.</Text>
</View>
);
}
constructor() {
super();
}
}
AppRegistry.registerComponent('Component1', () => Component1);
Try this path to your component
import Component1 from './app/components/Component1/component1';
Embarrassing question, but here we go.
I have a layout.js file that is supposed to render out my React components.
Layout.j:s
import React, { Component } from 'react'
import MainNavbar from './Navbar'
import AddButton from './AddButton'
export default class Layout extends Component {
render() {
return (
<div>
<Navbar />
<AddButton />
</div>
)
}
}
The Navbar component works fine, but my AddButton wont display at all. I've tried console.log-ging from it and nothing gets outputted in the console. So I guess my problem has to do with my component not getting linked.
Here's the file that won't get displayed:
AddButton.js:
import React, { Component } from 'react'
import { Button } from 'react-bootstrap'
export default class AddButton extends Component {
render() {
console.log('hejsan')
return (
<div>
helloooooo
</div>
)
}
}
Here is the index.js file:
index.js
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import Layout from './components/Navbar'
const app = document.getElementById('root')
ReactDOM.render(<Layout />, app)
I bet my problem is super basic, but I have no idea how to resolve this.
Thanks for reading!
So the mistake is very basic, you have imported Layout from Navbar which is nothing but your Navbar component and hence only Navbar is rendered, rather you should have imported it from Layout.js
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import Layout from './components/Layout'
const app = document.getElementById('root')
ReactDOM.render(<Layout />, app)
Also in Layout.js change
import MainNavbar from './Navbar'
to
import Navbar from './Navbar'
When I am using create react app when compliling I get the following errors ./src/Grid.js
"Line 3: Your render method should have return statement" and "Line 5: 'render' is not defined"
this is the contents of the the Grid.js file
import React, { Component } from 'react';
class Grid extends Component {
render(){
return <p>Hello, World</p>;
}
}
export default Grid;
I can't see that is wrong here? any help appreciated
import React, { Component } from 'react';
import Grid from './Grid.js';
class App extends Component
{
render()
{
return(
<div>
<Grid />
</div>
);
}
}
export default App;
This is where I am using the component
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
Where ReactDOM render is used
You are missing import {render} from 'react-dom';.
It should be:
import React, { Component } from 'react';
class Grid extends Component {
render(){
return(<p>Hello, World</p>);
}
}
export default Grid;
The above quote no longer applies.
I tried your exact code for Grid.js and App.js from the question and it works well on my machine. Whatever reason why your thing isn't working isn't in your Grid.js or App.js.
Still no real explanation for this. Copying the component code in to a newly created file compiles correctly. Very strange indeed.