【发布时间】:2021-09-15 20:01:18
【问题描述】:
我有一个只返回子类的类
class Name extends PureComponent {
constructor(props) {
super(props);
this.text = "meow";
}
render() {
return this.props.children;
}
}
我在我的 JSX 中创建了组件,其中的文字说我的 Classes Prop “text”
<Name>
<Text>{Name.text}</Text>
</Name>
但错误提示 Name.text 不存在,您的意思是访问静态属性吗?
我如何使用 JSX 标签来做 new Name() 然后使用 React/React Native 访问里面的信息?
编辑 1 更好的例子
interface User {
data: object;
firstName: string;
lastName: string;
}
class User extends PureComponent {
constructor(props: object) {
super(props);
fetch("https://randomuser.me/api/")
.then((response) => response.json())
.then((data) => this.data);
this.firstName = this.data.results.name.first;
this.lastName = this.data.results.name.last;
}
render() {
return this.props.children;
}
}
<User>
<Text>{User.firstName}</Text>
</User>
【问题讨论】:
-
您确定某个组件是适合您的工具吗?您似乎没有使用组件提供的任何东西。
-
是的,我正在使用 React Native,所以我真的没有选择,但我这样做了,所以任何需要使用该信息的东西都将放在标签
下 -
@NicholasTower 我用一个更好的例子编辑了这篇文章我的班级会是什么样子并且使用
标签你可以调用 User.firstName 等
标签: javascript node.js reactjs react-native web