我已经用类组件尝试过这个:
class Foo extends React.Component {
x = 3;
componentDidMount () {
fetch(apiURL).then(() => {
x = 5;
});
}
render () {
return <div>{x}</div>;
}
}
并使用功能组件:
let x = 3;
fetch(apiURL).then(() => {
x = 5;
});
const Foo = () => <div>{x}</div>;
页面上显示的 x 值永远不会改变,或者似乎是随机改变的。是什么赋予了?