【发布时间】:2020-03-26 20:29:07
【问题描述】:
我正在使用 React 16.13.0。我已经在我的组件中定义了这个静态块...
class FormContainer extends Component {
statics: {
DEFAULT_COUNTRY: 484;
}
constructor(props) {
super(props);
...
componentDidMount() {
let initialCountries = [];
let initialProvinces = [];
// Get initial countries
fetch('/countries/')
.then(response => {
return response.json();
}).then(data => {
initialCountries = data.map((country) => {
return country
});
console.log("output ...");
console.log(initialCountries);
this.setState({
countries: initialCountries,
});
});
// Get initial provinces (states)
console.log("val:" + this.DEFAULT_COUNTRY);
我的问题是,如何引用该静态块?以上
console.log("val:" + this.DEFAULT_COUNTRY);
生产
undefined
【问题讨论】:
-
你期待某种特定的 React 特性吗?您似乎只是在 TypeScript 语法中定义公共属性的类型。
-
我只是想知道如何访问静态变量的值。如果它与 React 无关,那就更好了。
-
不过,这 不是 静态变量,因此是我的问题。我不确定你从哪里知道
statics: {...}是你想要的东西 - 你能分享一下吗? -
试试
public static DEFAULT_COUNTRY = 484; -
感谢@johnannchopin,我可能应该提到这是一个 .jsx 文件。也许出于这个原因,当我尝试您的建议时,我收到编译错误“解析错误:意外标记”,并且编译错误标记指向“静态”一词。
标签: reactjs static constants instance-methods