我有一些輸入來創建/更新公式,1)如果 value={question},一切正常,除了我不能刪除最后一個字符(=輸入的第一個字符)2)如果我不提及值,那一切都很好,除了當我想用雪佛龍圖標按鈕更改問題順序時,數據庫已經很好地改變了,但輸入值仍然顯示在最后一個地方。<input type="text" value={question} placeholder="blabla" onChange={event => { event.preventDefault(); const value = event.target.value; setUpdatedQuestion(value); }} />我嘗試將 if (event.target.value == "" || event.target.value) 添加到 onChange 但也不起作用好的,我找到了一些東西,但它是一個 McGyver 提示,不是很干凈:在所有新添加問題之前添加一個空格啊啊。但后來我再也看不到我的占位符了:/僅供參考:問題來自 questions.map,setUpdatedQuestion 更新問題,newOrder 也更新問題//in QuestionsContent.js (questions is a questions tab)const QuestionsContent = props => {return ( <form onSubmit={onSubmit}> {isLoading === false && questions.length > 0 ? questions.map((question, i) => { return ( <div key={i}> <QuestionLine {...question} questions={questions} setQuestions={setQuestions} setNewOrder={setNewOrder} /> </div> ); }) : null} <button onClick={event => { event.preventDefault(); setAddQuestion({ question: null, type: "texte" }); }} > Add a question </button></form>);};//in QuestionLine.js:const QuestionLine = ({ question, setNewOrder, setQuestions, questions}) => { const [updatedQuestion, setUpdatedQuestion] = useState(""); // * UPDATE QUESTION ******************************* useEffect(() => { if (updatedQuestion !== "") { const tab = []; for (let j = 0; j < questions.length; j++) { if (j === i) { const newObject = { question: updatedQuestion, type: type }; console.log("adding updatedQuestion ===>", newObject); tab.push(newObject); } else { tab.push(questions[j]); } }感謝您的寶貴幫助
反應鉤子:輸入onChange第一個字符不能刪除
墨色風雨
2022-05-26 11:08:22