2 回答
TA贡献1817条经验 获得超6个赞
您必须使用onChange
event 并从event.target
.
你有例子:
function App() {
function handleExchange(e) {
console.log(e.target.value);
}
return (
<div>
<input className="input-r" placeholder="Type here" onChange={handleExchange} />
{/*or*/}
{/* onKeyPress={handleExchange(event) */}
</div>
)
}
ReactDOM.render(<App />, document.getElementById("root"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
TA贡献1794条经验 获得超8个赞
onChange向input元素添加处理程序。注意:处理程序传递 ,event因此您需要从事件目标中获取值。在这里,我使用了destructuring。
function App() {
function handleExchange(e) {
const { target: { value } } = e;
console.log(value)
}
return (
<input
className="input-r"
placeholder="Type here"
onChange={handleExchange}
/>
);
}
ReactDOM.render(<App /> , document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
添加回答
举报