Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 以这种方式使用useEffect有什么问题吗?_Reactjs - Fatal编程技术网

Reactjs 以这种方式使用useEffect有什么问题吗?

Reactjs 以这种方式使用useEffect有什么问题吗?,reactjs,Reactjs,以这种方式使用useHooks处理componentDidMount和componentDidUpdate是否存在任何潜在问题 这里有两个目标: 使用一个useffect来处理componentDidMount和componentdiddupdate 无需传入第二个参数(通常是带有道具的数组) const once=useRef(false) useffect(()=>{ if(once.current==false){ once.current=true //按组件的方式做事 返回 } //按

以这种方式使用useHooks处理componentDidMount和componentDidUpdate是否存在任何潜在问题

这里有两个目标:

  • 使用一个
    useffect
    来处理
    componentDidMount
    componentdiddupdate

  • 无需传入第二个参数(通常是带有道具的数组)

  • const once=useRef(false)
    useffect(()=>{
    if(once.current==false){
    once.current=true
    //按组件的方式做事
    返回
    }
    //按照componentDidUpdate中的方式进行操作
    //清理
    return()=>{
    //
    }
    
    })//没有问题,但如果任何道具或状态值将更改,则useEffect将不会在组件重新渲染时加载

    所以,当这取决于值的变化时,最好使用第二个参数

    const once = useRef(false)
    useEffect(() => {
    
        if(once.current === false){
            once.current = true
            // do things as were in componentDidMount
            return
        }
    
        // do things as were in componentDidUpdate
    
        // clean up
        return () => {
            //
        }
    },[]) // some value as array who will change and this need to be called 
    

    它没有问题,但如果任何道具或状态值将更改,则useEffect将不会在组件重新渲染时加载

    所以,当这取决于值的变化时,最好使用第二个参数

    const once = useRef(false)
    useEffect(() => {
    
        if(once.current === false){
            once.current = true
            // do things as were in componentDidMount
            return
        }
    
        // do things as were in componentDidUpdate
    
        // clean up
        return () => {
            //
        }
    },[]) // some value as array who will change and this need to be called 
    

    Yopu应将空数组作为参数传递

    useffect(()=>{
    if(once.current==false){
    //按组件的方式做事
    once.current=true
    }
    return()=>{
    //此区域将在组件卸载时激发
    }
    },[])//通过提供一个空数组,此useEffect的作用类似于componentDidMount
    
    如果要在状态更改时重新呈现useEffect,可以将状态添加到空数组中,以便它将侦听状态更改并执行

    const[someStateValue,setSomeStateValue]=useState(“”)
    useffect(()=>{
    ...
    },[someStateValue])//将在someStateValue更改时执行
    
    如果您想将componentDidMount和componentDidUpdate结合起来,我有一个解决方案

    const[val,setVal]=useState('asd')
    const[oldVal,setOldVal]=useState(“”)
    useEffect(()=>{//componentDidMount
    如果(val!==oldVal){//componentdiddupdate
    //把瓦尔传给老瓦尔
    setOldVal(val)
    //将新val设置为新val
    setVal(“theNewestVal”)
    }
    return()=>{//componentWillUnmount
    ...
    }
    },[val])//将在someStateValue更改时执行
    
    Yopu应该将空数组作为参数传递

    useffect(()=>{
    if(once.current==false){
    //按组件的方式做事
    once.current=true
    }
    return()=>{
    //此区域将在组件卸载时激发
    }
    },[])//通过提供一个空数组,此useEffect的作用类似于componentDidMount
    
    如果要在状态更改时重新呈现useEffect,可以将状态添加到空数组中,以便它将侦听状态更改并执行

    const[someStateValue,setSomeStateValue]=useState(“”)
    useffect(()=>{
    ...
    },[someStateValue])//将在someStateValue更改时执行
    
    如果您想将componentDidMount和componentDidUpdate结合起来,我有一个解决方案

    const[val,setVal]=useState('asd')
    const[oldVal,setOldVal]=useState(“”)
    useEffect(()=>{//componentDidMount
    如果(val!==oldVal){//componentdiddupdate
    //把瓦尔传给老瓦尔
    setOldVal(val)
    //将新val设置为新val
    setVal(“theNewestVal”)
    }
    return()=>{//componentWillUnmount
    ...
    }
    },[val])//将在someStateValue更改时执行
    
    这将导致效果在每次重新渲染时运行,这正是这里的目标,模仿
    componentDidUpdate
    过去所做的,这会是一个问题吗?在CDM中,您通常检查道具或状态并选择要做什么,最好将依赖项添加到数组中,如果没有任何更改,仍然可以重新渲染。我认为你应该编辑问题并说出你想做什么,提供更好的解决方案会更容易这将导致效果在每次重新渲染上运行,这正是这里的目标,模仿
    componentDidUpdate
    过去的做法,这会是个问题吗?在CDM中,你通常检查道具或状态并选择要做什么,最好将依赖项添加到数组中,如果没有任何更改,仍然可以进行重新渲染。我认为你应该编辑这个问题,说出你想做什么,提供更好的解决方案会更容易我以为useEffect会在第一次和每次重新渲染时加载而不传入第二个参数?我以为useEffect会在第一次和每次重新渲染时加载而不传入第二个参数?你所描述的实际上是我尝试的第一件事,但是后来我想为什么不把两者结合起来,这样就不那么混乱了呢?我明白你的意思,你想让useEffect与didMount和didUpdate同时结合起来,让我来看看it@DonKlein你尝试过解决方案吗?是的,我尝试过上面所有建议的方法,但仍然支持我最初的建议,为了让所有的副作用都集中在一个地方,而不是分散在各个条款中。你所描述的实际上是我尝试的第一件事,但后来我想为什么不把两者结合在一起,这样就不那么混乱了?我理解你的观点,您正在尝试将useEffect与didMount和didUpdate同时结合起来,让我来研究一下it@DonKlein你尝试过解决方案吗?是的,我尝试过上面所有建议的方法,但仍然支持我最初的建议,目的是将所有副作用集中在一个地方,而不是分散在各个条款中。