English 中文(简体)
在我的情况下,我有一副作用,没有能够迅速完成。
原标题:I have a async useEffect in my context that doesn t finish rendering fast enough

My context

export const UserContextProvider = ({children}) => {

    const [state, dispatch] = useReducer(authorizeUser, {loading: true})

    useEffect(() => {
        const user = localStorage.getItem( user )

        if(user){
            const decoded = jwtDecode(user)
            dispatch({type:  LOGIN , payload: decoded._id})
        }

        dispatch({type:  LOADING })
        

    }, [])

    if(state.loading){
        return (
          <div>
            <h1>Loading...</h1>
          </div>
        )
      }

    return(
        <userContext.Provider value={{...state, dispatch}}>
            {children}
        </userContext.Provider>
    )
}

I tried creating a loading state and using that to ensure that

my Landing page

return (
    !user ? <div><h1>loading...</h1></div> :
    <div>
      <div>
        <div>
          <h1>Welcome {user.name}</h1>
          <UserLogout></UserLogout>
        </div>
        <form onSubmit={handleSubmit}>
          <input
          type= file 
          name= photo 
          onChange={handlePhoto}
          >
          </input>
          <input type= submit ></input>
        </form>
      </div>
      <UserImageGallery image = {image} />
    </div> 
  ) 
}

export default LandingExample

As you can see I ve tried using to check loading and user but neither ends up working. It ends up just displaying "loading..." forever. How can I make sure that my component doesn t load until its mounted to avoid errors?

问题回答

This error is happening because you don t check condition for dispatch({type: LOADING }) exactly.

页: 1

export const UserContextProvider = ({children}) => {

  useEffect(() => {
    const user = localStorage.getItem( user )

    if(user){
      const decoded = jwtDecode(user)
      dispatch({type:  LOGIN , payload: decoded._id})
      return
    }
    dispatch({type:  LOADING })
  }, [])
}

Or else:

export const UserContextProvider = ({children}) => {

  useEffect(() => {
    const user = localStorage.getItem( user )

    if(user){
      const decoded = jwtDecode(user)
      dispatch({type:  LOGIN , payload: decoded._id})
    } else {
      dispatch({type:  LOADING })
    }
  }, [])
}




相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签