我正在尝试创建一个组件并在App.jsx (另一个组件)中调用它,并在呈现中调用这个App.jsx,但是它给了我错误。
错误:错误:App(.):render没有返回任何内容。这通常意味着缺少返回语句。或者,若要不呈现任何内容,请返回null。
这是我做的代码。
组件: (Greetings.Jsx)
import React from 'react'
function Greetings()
{
let timehours=new Date()
timehours=timehours.getHours();
let cssStyle={
color:'Green',
}
let text="";
if(timehours>=1&&timehours<=12)
{
// z.src = window.location.origin + '/images/morning.jpeg';
// document.body.backgroundImage= z.src;
text="Good Morning";
cssStyle.color='Green';
// bgimg.Image=window.location.origin + '/images/morning.jpg';
}
else if(timehours>=12&&timehours<19)
{
// bgimg.Image=window.location.origin + '/images/morning.jpg';
text="Good Afternoon";
cssStyle.color='Orange';
}
else
{
text="Good Night";
cssStyle.color='Black';
}
return(
<>
<div>
<h1> Hello Sir, <span style={cssStyle}>{text}</span></h1>
</div>
</>
);
}
export default Greetings;App.Jsx
import React from 'react';
import Greetings from './Greetings'
function App()
{
return
(
<>
<Greetings/>
</>
);
}
export default App;Index.jsx
import React from 'react';
import reactDom from 'react-dom';
import ReactDOM from 'react-dom';
import "./index.css";
import App from "./App";
ReactDOM.render(<App/>,document.getElementById("root"),);发布于 2021-04-07 11:21:58
我刚刚重新创建了App.jsx文件,它是
import React from 'react'
import Greetings from './Greetings'
function App()
{
return(<Greetings></Greetings>);
}
export default App;现在为我工作!
发布于 2021-04-07 11:22:08
不需要空括号,在App.Jsx中,return后面的副词必须位于同一行(特别感谢@Martin):
Greeting.jsx:
import React from 'react'
function Greetings()
{
// the other code is omitted for the brevity
return(
<div>
<h1> Hello Sir, <span style={cssStyle}>{text}</span></h1>
</div>
);
}
export default Greetings;以及:
import React from 'react';
import Greetings from './Greetings'
function App()
{
return <Greetings/>;
}
export default App;https://stackoverflow.com/questions/66984973
复制相似问题