2 回答
![?](http://img1.sycdn.imooc.com/545845d30001ee8a02200220-100-100.jpg)
TA贡献1784条经验 获得超8个赞
您需要调用处理函数并返回要呈现的 HTML。此外,如果您尝试更新变量并渲染它,请使用setState基于类的组件或[stateName, stateUpdateMethod] = useState(null)功能组件,然后用于stateUpdateMethod触发重新渲染。
import React from "react";
import ReactDOM from "react-dom";
const handler = (text) => <div>{text}</div>
const App = () => handler("I am the text")
const rootElement = document.getElementById("root");
ReactDOM.render(
<App />,
rootElement
);
![?](http://img1.sycdn.imooc.com/5458477300014deb02200220-100-100.jpg)
TA贡献1779条经验 获得超6个赞
您使用变量的方式不正确。
我在这里写了一个类似问题的答案。它显示了如何更新视图以及如何声明变量。
请检查那里的答案,如果您仍有疑问,请告诉我。
以下是那里答案的简短摘要:
如果您也想更新视图(这是您的情况),您应该在设置或更改值时使用 state 和 setState 方法。
例子:
class MyContainer extends Component {
state = { testVarible: "this is a test" };
onMove = () => {
console.log(this.state.testVarible);
this.setState({ testVarible: "new value" });
}
}
话虽如此,我仍然觉得你让它变得复杂。
你应该做的是这个。
state = { text: 'some text' };
const handler = (text) => {
this.setState({ text });
};
虽然 jsx 应该有这个:
<div className="other">
<li>
<span className="bold">Other: </span>{this.state.text}
<span className="right">Price Unknown</span>
</li>
</div>
添加回答
举报