[英]javascript - export default/import not working
This is my code: Why is this not working?这是我的代码:为什么这不起作用? The code is suppose to show the
h1
in the html file, but for some reason it doesn't do that for me.该代码假设在 html 文件中显示
h1
,但由于某种原因,它对我没有这样做。
Element.js:元素.js:
import React from "react"
function Element() {
return (
<div>
<h1>Hello World</h1>
</div>
)
}
export default Element
app.js:应用程序.js:
import React from 'react'
import ReactDOM from 'react-dom'
import Element from './Element.js'
ReactDOM.render(<Element />, document.getElementById('root'))
the html code: html 代码:
<script src="/index.js" type="text/babel"></script>
<div id="root"></div>
I looked at many solutions at stackoverflow but this bug it still doesn't work.我在 stackoverflow 上查看了许多解决方案,但这个错误仍然无法正常工作。 I added
import React from "react"
and import ReactDOM from "react-dom"
in both files but still same thing: nothing in the return
statement doesn't show up, I've been struggling for hours.我在两个文件中都添加了
import React from "react"
和import ReactDOM from "react-dom"
但还是一样的: return
语句中的任何内容都没有出现,我已经挣扎了好几个小时。 What is wrong with my code?我的代码有什么问题?
It appears that there are three issues here.这里似乎存在三个问题。 With the code provided, thats what I get.
使用提供的代码,这就是我得到的。
type
attribute needs a change.type
属性需要更改。 You can completely remove it too.render
method.render
方法。 You have to fitst create a root, and then render the app like soconst root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Element />);
root
div element, coz the script when gets executed may not know the element with id
root.root
div 元素之后包含脚本,因为执行时的脚本可能不知道id
为 root 的元素。I would re-do the app.js to我会重新做 app.js 到
import Element from './test' function App() { return ( <div className='App'> <Element /> </div> ) } export default App
PS. PS。 also some links to the official documentation and how to use react https://reactjs.org/docs/create-a-new-react-app.html https://reactjs.org/docs/rendering-elements.html
also some links to the official documentation and how to use react https://reactjs.org/docs/create-a-new-react-app.html https://reactjs.org/docs/rendering-elements.html
PS 2: or if You would really like to do it in the old way PS 2:或者如果您真的想以旧方式进行操作
import React from 'react' import './App.css' import Element from './test' function App() { return React.createElement('div', {}, React.createElement(Element)) } export default App
Following things need to improve into your code-以下事情需要改进到您的代码中 -
1-Use of root level rendering things in index.js instead of app.js. 1-在 index.js 而不是 app.js 中使用根级渲染事物。
index.js index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App.js";
ReactDOM.render(
<App /> ,
document.getElementById("root")
);
2-Use App.js file to accumulate all other component into it. 2-使用 App.js 文件将所有其他组件累积到其中。
App.js应用程序.js
import Element from './Element'从'./Element'导入元素
function App() {
return (
<div className='App'>
<Element />
</div>
)
}
PS: Learn the heirarchy of react and its flow. PS:了解 React 的层次结构及其流程。 This links may be helpful:
此链接可能会有所帮助:
React Rendering - https://reactjs.org/docs/rendering-elements.html反应渲染 - https://reactjs.org/docs/rendering-elements.html
Introduction to react rendering https://medium.com/information-and-technology/an-introduction-to-react-rendering-9c24a96b838b React渲染介绍https://medium.com/information-and-technology/an-introduction-to-react-rendering-9c24a96b838b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.