Textarea的默认值在保留`onchange`属性时不会显示。

huangapple go评论74阅读模式
英文:

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 = () =&gt; {

    const notes = [
        {id:1,
         title:&quot;What is React? &quot;,
        content:&quot;React is a JavaScript library that aims to simplify development of visual interfaces.&quot;,
        // date:&quot;28/04/2023&quot;
        },
        {
         id:2,
         title:&quot;What is Java?&quot;,
         content:&quot;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.&quot;,
        //  date:&quot;28/04/2023&quot;
        }
    ]
    const [mynote, setNote] = useState(notes);
    const [isActive,setisActive] = useState([]);
  
    const handleClick = (id) =&gt;{
       setisActive(id);
    }

    const pullcontent = (id,plnote) =&gt;{
      console.log(&quot;pull Content for-&gt;&quot;+id+&quot;...&quot;+plnote);
      setNote(mynote.map(obj =&gt;{
        if( obj[&#39;id&#39;] === parseInt(id)){
          return {...obj,content:plnote}
        }else{
          return obj;
        }
      }));
    }

  return (
    &lt;div className=&#39;main&#39;&gt;
    &lt;div className=&quot;rightnote note-cont&quot;&gt;
      &lt;div className=&quot;contents&quot;&gt;
      {
        mynote.map((obj)=&gt;{
            return (
         &lt;div className={`conts ${isActive === obj.id ? &#39;cont-active&#39;:&#39;cont-inactive&#39;}`} key={obj.id}&gt;
                &lt;div className=&quot;con&quot;&gt;
                &lt;Content notecontent = {obj.content} noteid={obj.id} onCh={(pullednote) =&gt; pullcontent(obj.id,pullednote)}/&gt;
                &lt;/div&gt;
             &lt;/div&gt;
            )
        })
      }
      &lt;/div&gt; 
        &lt;/div&gt;

    &lt;/div&gt;
  )
}

export default Main

<!-- end snippet -->

Child

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import React, { useState } from &#39;react&#39;;
import &#39;./Content.css&#39;;

const Content = (Props,{notecontent,noteid}) =&gt; {

  const [note,setnote] = useState(notecontent);

  const changeHandler = (e) =&gt;{
    setnote(e.target.value);
    Props.onCh(note);
  }

  return (
    &lt;div className=&quot;content&quot;&gt;
      &lt;textarea name=&quot;&quot; id={noteid}  defaultValue={notecontent} cols=&quot;164&quot; rows=&quot;48&quot; contentEditable={true} onChange={(e)=&gt;changeHandler(e)} suppressContentEditableWarning={true}/&gt;
      &lt;/div&gt;
  )
}

export default Content;  

<!-- end snippet -->

答案1

得分: 0

以下是您要翻译的部分:

"Your code seems to be working if you make these small changes:

// const Content = (Props,{notecontent,noteid}) =&gt; { // BEFORE:
const Content = ({onCh, notecontent,noteid}) =&gt; { 

  const [note,setnote] = useState(notecontent);

  const changeHandler = (e) =&gt;{
    setnote(e.target.value);
    onCh(note); // &lt;-- Props.onCh() -&gt; onCh()
  }
```"

<details>
<summary>英文:</summary>

Your code seems to be working if you make these small changes:

```js
// const Content = (Props,{notecontent,noteid}) =&gt; { // BEFORE:
const Content = ({onCh, notecontent,noteid}) =&gt; { 

  const [note,setnote] = useState(notecontent);

  const changeHandler = (e) =&gt;{
    setnote(e.target.value);
    onCh(note); // &lt;-- Props.onCh() -&gt; onCh()
  }

答案2

得分: 0

First change this

将这部分首先更改:

const Content = (Props,{notecontent,noteid}) =&gt; {

to

更改为

const Content = ({onCh,notecontent,noteid}) =&gt; {

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) =&gt;{
    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) =&gt;{
    setnote(e.target.value);
    onCh(note);
    // 在此处传递时,note的值没有设置
}
// 此函数执行完毕后,然后传递

There are 2 solutions to this

有两种解决方案:

  1. Just pass the e.target.value to the Props.onCh function

1)只需将e.target.value传递给Props.onCh函数

const changeHandler = (e) =&gt;{
    setnote(e.target.value);
    onCh(e.target.value);
}
  1. use flushSync

2)使用flushSync

import { flushSync } from &#39;react-dom&#39;;

...

const changeHandler = (e) =&gt;{
    flushSync(()=&gt;{
    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}) =&gt; {

to

const Content = ({onCh,notecontent,noteid}) =&gt; {

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) =&gt;{
    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

  1. Just pass the e.target.value to the Props.onCh function
const changeHandler = (e) =&gt;{
    setnote(e.target.value);
    onCh(e.target.value);
}
  1. use flushSync
import { flushSync } from &#39;react-dom&#39;;

...

const changeHandler = (e) =&gt;{
    flushSync(()=&gt;{
    setnote(e.target.value);
    onCh(note);
    })
}

But, I would recommend the first way as flushSync is performance heavy.

huangapple
  • 本文由 发表于 2023年7月6日 22:45:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定