使用axios与reducer / context / provider一起使用。

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

using axios with reducer / context / provider

问题

I'm using useEffect to make an api call to set initialState, then pass it to "todo" with useReducer

code here:

export function TodoProvider({children}){

const apiUrl = "http://localhost:3001/home";

const [initialState,setInitialState] = useState([]);

useEffect(()=>{
axios.get(apiUrl)
.then((response)=>{
setInitialState(response.data.data);
})
.catch(e=>console.log(e))
},[]);

console.log(initialState);

const [todo, dispatch] = useReducer(todoReducer, initialState);

return (
<TodoContext.Provider value={todo}>
<TodoDispatchContext.Provider value={dispatch}>
{children}
</TodoDispatchContext.Provider>
</TodoContext.Provider>
)
}

but todo isn't getting the value of initialState, does anyone know what am i doing wrong here?

is it because useReducer() only gets the first time value from initialState? is there a different way to do it?

and in the inspector in chrome
that one console.log(initialState) prints these:
使用axios与reducer / context / provider一起使用。
thank you in advance

英文:

I'm using useEffect to make an api call to set initialState, then pass it to "todo" with useReducer

code here:

export function TodoProvider({children}){

  const apiUrl = &quot;http://localhost:3001/home&quot;;

  const [initialState,setInitialState] = useState([]);

  useEffect(()=&gt;{
    axios.get(apiUrl)
    .then((response)=&gt;{
        setInitialState(response.data.data);
    })
    .catch(e=&gt;console.log(e))
  },[]);

  console.log(initialState);

  const [todo, dispatch] = useReducer(todoReducer, initialState);


  return (
      &lt;TodoContext.Provider value={todo}&gt;
        &lt;TodoDispatchContext.Provider value={dispatch}&gt;
            {children}
        &lt;/TodoDispatchContext.Provider&gt;
      &lt;/TodoContext.Provider&gt;
    )
  }

but todo isn't getting the value of initialState, does anyone know what am i doing wrong here?

is it because useReducer() only gets the first time value from initialState? is there a different way to do it?

and in the inspector in chrome
that one console.log(initialState) prints these:
使用axios与reducer / context / provider一起使用。
thank you in advance

答案1

得分: 1

问题似乎是由于传递给useReducerinitialState保持为空数组而引起的。

您可以更新状态和reducer声明如下:

const [initialState, setInitialState] = useState(null);

const [todo, dispatch] = useReducer(todoReducer, initialState || []);

并且有条件地进行渲染,如下所示:

if (initialState === null) {
  return <div>Loading...</div>;
}

return (
  <TodoContext.Provider value={todo}>
    <TodoDispatchContext.Provider value={dispatch}>
      {children}
    </TodoDispatchContext.Provider>
  </TodoContext.Provider>
);

完整代码如下:

export function TodoProvider({ children }) {
  const apiUrl = "http://localhost:3001/home";

  const [initialState, setInitialState] = useState(null);

  useEffect(() => {
    axios
      .get(apiUrl)
      .then((response) => {
        setInitialState(response.data.data);
      })
      .catch((error) => {
        console.log(error);
      });
  }, []);

  const [todo, dispatch] = useReducer(todoReducer, initialState || []);

  if (initialState === null) {
    return <div>Loading...</div>;
  }

  return (
    <TodoContext.Provider value={todo}>
      <TodoDispatchContext.Provider value={dispatch}>
        {children}
      </TodoDispatchContext.Provider>
    </TodoContext.Provider>
  );
}
英文:

The issue seem to be caused due to the initialState passed to useReducer staying as an empty array.

You can update the state and reducer declaration as:

  const [initialState, setInitialState] = useState(null);

  const [todo, dispatch] = useReducer(todoReducer, initialState || []);

and conditionally render, as:

  if (initialState === null) {
    return &lt;div&gt;Loading...&lt;/div&gt;;
  }

  return (
    &lt;TodoContext.Provider value={todo}&gt;
      &lt;TodoDispatchContext.Provider value={dispatch}&gt;
        {children}
      &lt;/TodoDispatchContext.Provider&gt;
    &lt;/TodoContext.Provider&gt;
  );

Full code:

export function TodoProvider({ children }) {
  const apiUrl = &quot;http://localhost:3001/home&quot;;

  const [initialState, setInitialState] = useState(null);

  useEffect(() =&gt; {
    axios
      .get(apiUrl)
      .then((response) =&gt; {
        setInitialState(response.data.data);
      })
      .catch((error) =&gt; {
        console.log(error);
      });
  }, []);


  const [todo, dispatch] = useReducer(todoReducer, initialState || []);


  if (initialState === null) {
    return &lt;div&gt;Loading...&lt;/div&gt;;
  }

  return (
    &lt;TodoContext.Provider value={todo}&gt;
      &lt;TodoDispatchContext.Provider value={dispatch}&gt;
        {children}
      &lt;/TodoDispatchContext.Provider&gt;
    &lt;/TodoContext.Provider&gt;
  );
}

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

发表评论

匿名网友

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

确定