英文:
Textarea default value not coming when keeping onchange attribute
问题
以下是您要翻译的内容:
Parent
我有两个组件,父组件和子组件,在子组件中我有一个文本区域,当其发生变化时,会重置我的初始值。
如果在文本区域中保留 onchange 属性,那么默认值将被移除。
最初应该显示默认值,一旦更改,文本应该更改。
父组件
Child
子组件
英文:
I have two components parent and child,in child i have the text area, which onchange will reset my initial value.
On keeping onchange attribute in the textarea , the default value is being removed
Initially it should show default value, upon changing it should change the text.
Parent
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const Main = () => {
const notes = [
{id:1,
title:"What is React? ",
content:"React is a JavaScript library that aims to simplify development of visual interfaces.",
// date:"28/04/2023"
},
{
id:2,
title:"What is Java?",
content:"Java is a most popular, object-oriented, widely used programming language and platform that is utilized for Android development, web development, artificial intelligence, cloud applications, and much more.",
// date:"28/04/2023"
}
]
const [mynote, setNote] = useState(notes);
const [isActive,setisActive] = useState([]);
const handleClick = (id) =>{
setisActive(id);
}
const pullcontent = (id,plnote) =>{
console.log("pull Content for->"+id+"..."+plnote);
setNote(mynote.map(obj =>{
if( obj['id'] === parseInt(id)){
return {...obj,content:plnote}
}else{
return obj;
}
}));
}
return (
<div className='main'>
<div className="rightnote note-cont">
<div className="contents">
{
mynote.map((obj)=>{
return (
<div className={`conts ${isActive === obj.id ? 'cont-active':'cont-inactive'}`} key={obj.id}>
<div className="con">
<Content notecontent = {obj.content} noteid={obj.id} onCh={(pullednote) => pullcontent(obj.id,pullednote)}/>
</div>
</div>
)
})
}
</div>
</div>
</div>
)
}
export default Main
<!-- end snippet -->
Child
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import React, { useState } from 'react';
import './Content.css';
const Content = (Props,{notecontent,noteid}) => {
const [note,setnote] = useState(notecontent);
const changeHandler = (e) =>{
setnote(e.target.value);
Props.onCh(note);
}
return (
<div className="content">
<textarea name="" id={noteid} defaultValue={notecontent} cols="164" rows="48" contentEditable={true} onChange={(e)=>changeHandler(e)} suppressContentEditableWarning={true}/>
</div>
)
}
export default Content;
<!-- end snippet -->
答案1
得分: 0
以下是您要翻译的部分:
"Your code seems to be working if you make these small changes:
// const Content = (Props,{notecontent,noteid}) => { // BEFORE:
const Content = ({onCh, notecontent,noteid}) => {
const [note,setnote] = useState(notecontent);
const changeHandler = (e) =>{
setnote(e.target.value);
onCh(note); // <-- Props.onCh() -> onCh()
}
```"
<details>
<summary>英文:</summary>
Your code seems to be working if you make these small changes:
```js
// const Content = (Props,{notecontent,noteid}) => { // BEFORE:
const Content = ({onCh, notecontent,noteid}) => {
const [note,setnote] = useState(notecontent);
const changeHandler = (e) =>{
setnote(e.target.value);
onCh(note); // <-- Props.onCh() -> onCh()
}
答案2
得分: 0
First change this
将这部分首先更改:
const Content = (Props,{notecontent,noteid}) => {
to
更改为
const Content = ({onCh,notecontent,noteid}) => {
After this your typed text will be one character less
在此之后,您输入的文本将少一个字符
This happens because you are using setnote and then passing the note value right after it.
这是因为您使用了setnote,然后立即传递了note值。
The way react works is that when you have setState calls inside a function the value is only updated when the function has finished execution.
So in this case
React的工作方式是,当您在函数内部调用setState时,只有在函数执行完成后值才会更新。
因此,在这种情况下
const changeHandler = (e) =>{
setnote(e.target.value);
onCh(note);
// The value of the note is not set here when you pass it
}
// After this function has finished execution then it is passed
const changeHandler = (e) =>{
setnote(e.target.value);
onCh(note);
// 在此处传递时,note的值没有设置
}
// 此函数执行完毕后,然后传递
There are 2 solutions to this
有两种解决方案:
- Just pass the
e.target.value
to theProps.onCh
function
1)只需将e.target.value
传递给Props.onCh
函数
const changeHandler = (e) =>{
setnote(e.target.value);
onCh(e.target.value);
}
- use
flushSync
2)使用flushSync
import { flushSync } from 'react-dom';
...
const changeHandler = (e) =>{
flushSync(()=>{
setnote(e.target.value);
onCh(note);
})
}
But, I would recommend the first way as flushSync is performance heavy.
但是,我建议使用第一种方法,因为flushSync性能较重。
英文:
First change this
const Content = (Props,{notecontent,noteid}) => {
to
const Content = ({onCh,notecontent,noteid}) => {
After this your typed text will be one character less
This happens because you are using setnote and then passing the note value right after it.
The way react works is that when you have setState calls inside a function the value is only updated when the function has finished execution.
So in this case
const changeHandler = (e) =>{
setnote(e.target.value);
onCh(note);
// The value of the note is not set here when you pass it
}
// After this function has finished execution then it is passed
There are 2 solutions to this
- Just pass the
e.target.value
to theProps.onCh
function
const changeHandler = (e) =>{
setnote(e.target.value);
onCh(e.target.value);
}
- use
flushSync
import { flushSync } from 'react-dom';
...
const changeHandler = (e) =>{
flushSync(()=>{
setnote(e.target.value);
onCh(note);
})
}
But, I would recommend the first way as flushSync is performance heavy.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论