3 回答

TA貢獻1830條經驗 獲得超3個贊
問題
由于onChange在每次按鍵時運行,因此您將quotation標記附加到每個按鍵。
解決方案
而不是把你的引號放在你的狀態上。在 JSX 中附加引號。
import React, { useState } from 'react';
const App = () => {
const [user, setUser] = useState('');
const onChange = (event) => {
setUser(event.target.value)
};
return (
<form>
<h3>Username is: {user ? `"${user}"`: ''}</h3>
<input
type="text"
value={user}
onChange={onChange} />
</form>
);
};
export default App;
從輸入字段傳遞額外參數
<input
onChange={e => onChange(e, 'hello, 'world')}
/>

TA貢獻1942條經驗 獲得超3個贊
When you use arrow functions there is no need to bind.
const onChange = (val)=>{
console.log(val)// comment
}
<input type="text"
onChange={() => onChange("comment")}
/>

TA貢獻1864條經驗 獲得超2個贊
我使用 coffeescript,你可以將 -> 視為函數或 =>,如果我們通過循環將參數傳遞給功能組件中的 onClick 事件處理程序:
onClick = (n, x) -> ->
setcheckindex n
x()
<Boxr onClick={onClick n, x.onClick} /> for x,n in g
我已經通過 n 發送了循環索引,并通過 x.onClick 發送了一個函數
添加回答
舉報