[英]Is it possible to insert an incomplete jsx snippet into a render function of another jsx file?
Say I have file Incomplete.jsx
and Main.jsx
说我有
Incomplete.jsx
和Main.jsx
文件
Incomplete.jsx
looks as follows: Incomplete.jsx
如下所示:
<Hey text="Hello World!" />
<Hey text="You made it to the end of the world!" />
Main.jsx
looks as follows: Main.jsx
如下所示:
import React, { Component } from 'react';
import './App.css';
import playlist from './Incomplete';
class Hey extends Component {
constructor(props) {
super(props);
}
render() {
const {text} = this.props;
return (
<p>{text}</p>
);
}
}
class App extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div>{playlist}</div>
);
}
}
export default App;
Then how can I make the program in such a way, that React will render 然后我如何以这样的方式制作程序,使得React可以渲染
render(){
return (
<div>
<Hey text="Hello World!" />
<Hey text="You made it to the end of the world!" />
</div>
);
}
Instead of 代替
render() {
return (
<div>{playlist}</div>
);
}
by importing Incomplete.jsx
? 通过导入
Incomplete.jsx
吗?
Because right now the code doesn't work. 因为现在代码不起作用。
And is something like this even possible? 这样的事情甚至可能吗? I know that if I create
Incomplete.jsx
, then I will get a lot of errors. 我知道如果创建
Incomplete.jsx
,那么我会得到很多错误。 I see components as functions, so the idea is that Incomplete.jsx
is a file with all the 'function calls' and the definition will be in Main.jsx
. 我将组件视为函数,因此想法是
Incomplete.jsx
是具有所有“函数调用”的文件,并且定义将在Main.jsx
。
One constraint that I have is that I don't want to do any imports in Incomplete.jsx
. 我的一个限制是我不想在
Incomplete.jsx
进行任何导入。
It's for a project that teaches children how to use some simple pre-defined React Components by simply calling them in the Incomplete.jsx
file. 这是一个教孩子如何通过简单地在
Incomplete.jsx
文件中调用一些简单的预定义React组件的项目。
Your incomplete.jsx
should look something like this: 您的
incomplete.jsx
应该如下所示:
const Playlist = (props) => {
return (
<div>
<Hey text="Hello World!" />
<Hey text="You made it to the end of the world!" />
</div>
)
}
export default Playlist;
If you have your import in your main.jsx
like this: import Playlist from './Incomplete';
如果您像这样在
main.jsx
import Playlist from './Incomplete';
: import Playlist from './Incomplete';
You can call your code by using <Playlist />
in the render()
. 您可以使用
render()
中的<Playlist />
来调用代码。
Because your incomplete
returns a component, and because we call it like that, you could add properties to it. 因为您的
incomplete
返回一个组件,并且因为我们这样称呼它,所以您可以为其添加属性。 Eg <Playlist amountOfNumbers={1} />
. 例如
<Playlist amountOfNumbers={1} />
。 Which you can retrieve from the props
field in your Playlist
您可以从
Playlist
的props
字段中检索
If the component you'd like to render inside your main.jsx
is supposed to have it's own state
. 如果您想在
main.jsx
呈现的组件应该具有自己的state
。 You can rewrite your Playlist
to something like this: 您可以将
Playlist
重写为以下形式:
export default class Playlist extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<Hey text="Hello World!" />
<Hey text="You made it to the end of the world!" />
</div>
);
}
}
The stateless (first) example, can be done without imports
. 无状态(第一个)示例可以不使用
imports
来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.