简体   繁体   English

useState 在 function 与组件 React 中的行为不同

[英]useState behaves differently in function vs componenet React

import React, {useState} from 'react'
import './box.css'
import cross from './cross.png';
import axios from 'axios'

function Box(props){
    const[disappear, setdisappear] = useState(false)
    const[appear, setappear] = useState(false)
    const[text, settext] = useState('')
    const[able, setable] = useState(true)
    

    async function clear(){
        console.log(props.id)
        setdisappear(true)
        let id = props.id
        let token = sessionStorage.getItem('token')
        let data =  {token:token, id:id}
        await axios.post('http://localhost:5000/home/deleteText', data)
        
        

    }
    function theText(e){
        let current = e.target.value
        console.log(current)
        settext(current)
        console.log(text)
        if(current===''){
            setable(true)
        }else{
            setable(false)
        }
        
    }
    async function savetext(){
        let data = {text:text, token:sessionStorage.getItem('token')}
        const request = await axios.post('http://localhost:5000/home/addText',data)
        //e.target.reset() 
        document.getElementById('textarea').value = ''
        
        setable(true)
        setappear(true)
        
    }
    function boxx(){
        return(
            <div className = 'wrapper'>
                <div className='outerbox'>
                    <div className='empty'>
                    </div>
                        <textarea id = 'textarea' onChange ={theText}  className='input' rows="10" cols="50" placeholder='enter ur text mf' defaultValue={props.text}></textarea>
                    <div className='x'>
                        <button onClick={clear} value={props.id} disabled = {props.disableButton}>
                            {props.disableButton?'':<img src={cross} alt = '' />}
                        </button>
                    </div>
                    {props.disableButton?<div><button disabled = {able} onClick = {savetext}>save</button></div>:''}
                </div>
            </div>
        )
    }
    return(
        <div>
            
            {disappear?null:boxx()}
            {appear?boxx():null}
        </div>
    )
}

export default Box

So, as you can see in the return statement of component Box, I call boxx function which returns the html of the box.因此,正如您在组件 Box 的返回语句中看到的那样,我调用 boxx function ,它返回盒子的 html。 In this case, if I input some text in the text area, the text gets printed as expected, however if I change the boxx function to Boxx and in the return statement I use it as <Boxx/> , the text I enter in the textarea gets deleted immediately or just one letter gets printed and after I input the second letter, they both get deleted.在这种情况下,如果我在文本区域中输入一些文本,文本会按预期打印,但是如果我将 boxx function 更改为 Boxx 并在返回语句中将其用作<Boxx/> ,我在textarea 会立即被删除,或者只打印一个字母,在我输入第二个字母后,它们都会被删除。 I figured out, this strange behaviour is because of useState in theText function but don't have a clue why this is happening.我想通了,这种奇怪的行为是由于 theText function 中的 useState 但不知道为什么会发生这种情况。 How can I solve this?我该如何解决这个问题?

Because Boxx is defined inside of Box , its component type changes every time Box re-renders, which causes React's diffing algorithm to reset the state of the <textarea> inside it.因为Boxx是在Box内部定义的,所以每次Box重新渲染时它的组件类型都会改变,这会导致 React 的diffing 算法重置其中<textarea>的 state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM