【发布时间】:2022-02-02 09:07:02
【问题描述】:
我正在尝试在我的应用程序的弹出窗口中创建一个简单的文本框。为了让用户输入文本,我创建了一个 useState 变量,并在 onChange 事件期间将状态更新为输入的值。然而,它并没有保持它的状态。代码如下:
const App() {
[units, setUnits] = useState('');
[popUpHtml, setPopUpHtml] = useState('');
...
function handleChange(event){
setUnits(event.target.value);
}
const betslip = (team, line) =>
(
<div className='betslip'>
<h1 className="betslip-header">Betslip</h1>
<div >
<table className="betslip-table">
<tr>
<th>Team</th>
<th>Line</th>
<th>Unit(s)</th>
</tr>
<tr >
<td className='betslip-td'>{team}</td>
<td className='betslip-td'>{linePlusMinus(line)}</td>
<td className='betslip-td'>
<div className='unit-div'>
<input type='text' required className='unit-input' value={units} onChange={(event) => handleChange(event)}/>
</div>
</td>
</tr>
</table>
<table className="betslip-table">
<tr>
<th>Risk</th>
<td className='betslip-td'>{units}</td>
</tr>
<tr>
<th>Reward</th>
<td className='betslip-td'>{reward(units, line)}</td>
</tr>
</table>
</div>
</div>
);
function showPopUp(team, line) {
setPopUpHtml(betslip(team, line));
setPopUpStyle('pop-up-container-show');
}
function PopUp() {
return (
<div id="popUp" className={popUpStyle}>
<div className="pop-up-bg"> </div>
<div className="pop-up">
<img className='close-button' src={closeButton} onClick={hidePopUp.bind(null)} />
{popUpHtml}
</div>
</div>
)
}
...
return (
<div className="container">
<PopUp html={<h1>Hello World </h1>} />
)
我正在尝试重新分配标签中的状态
【问题讨论】:
-
handleChange 在更改输入文本时触发?还有什么理由将 html (betslip return) 保持为状态?
标签: javascript html reactjs web