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

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

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:

  1. export function TodoProvider({children}){
  2. const apiUrl = &quot;http://localhost:3001/home&quot;;
  3. const [initialState,setInitialState] = useState([]);
  4. useEffect(()=&gt;{
  5. axios.get(apiUrl)
  6. .then((response)=&gt;{
  7. setInitialState(response.data.data);
  8. })
  9. .catch(e=&gt;console.log(e))
  10. },[]);
  11. console.log(initialState);
  12. const [todo, dispatch] = useReducer(todoReducer, initialState);
  13. return (
  14. &lt;TodoContext.Provider value={todo}&gt;
  15. &lt;TodoDispatchContext.Provider value={dispatch}&gt;
  16. {children}
  17. &lt;/TodoDispatchContext.Provider&gt;
  18. &lt;/TodoContext.Provider&gt;
  19. )
  20. }

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声明如下:

  1. const [initialState, setInitialState] = useState(null);
  2. const [todo, dispatch] = useReducer(todoReducer, initialState || []);

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

  1. if (initialState === null) {
  2. return <div>Loading...</div>;
  3. }
  4. return (
  5. <TodoContext.Provider value={todo}>
  6. <TodoDispatchContext.Provider value={dispatch}>
  7. {children}
  8. </TodoDispatchContext.Provider>
  9. </TodoContext.Provider>
  10. );

完整代码如下:

  1. export function TodoProvider({ children }) {
  2. const apiUrl = "http://localhost:3001/home";
  3. const [initialState, setInitialState] = useState(null);
  4. useEffect(() => {
  5. axios
  6. .get(apiUrl)
  7. .then((response) => {
  8. setInitialState(response.data.data);
  9. })
  10. .catch((error) => {
  11. console.log(error);
  12. });
  13. }, []);
  14. const [todo, dispatch] = useReducer(todoReducer, initialState || []);
  15. if (initialState === null) {
  16. return <div>Loading...</div>;
  17. }
  18. return (
  19. <TodoContext.Provider value={todo}>
  20. <TodoDispatchContext.Provider value={dispatch}>
  21. {children}
  22. </TodoDispatchContext.Provider>
  23. </TodoContext.Provider>
  24. );
  25. }
英文:

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:

  1. const [initialState, setInitialState] = useState(null);
  2. const [todo, dispatch] = useReducer(todoReducer, initialState || []);

and conditionally render, as:

  1. if (initialState === null) {
  2. return &lt;div&gt;Loading...&lt;/div&gt;;
  3. }
  4. return (
  5. &lt;TodoContext.Provider value={todo}&gt;
  6. &lt;TodoDispatchContext.Provider value={dispatch}&gt;
  7. {children}
  8. &lt;/TodoDispatchContext.Provider&gt;
  9. &lt;/TodoContext.Provider&gt;
  10. );

Full code:

  1. export function TodoProvider({ children }) {
  2. const apiUrl = &quot;http://localhost:3001/home&quot;;
  3. const [initialState, setInitialState] = useState(null);
  4. useEffect(() =&gt; {
  5. axios
  6. .get(apiUrl)
  7. .then((response) =&gt; {
  8. setInitialState(response.data.data);
  9. })
  10. .catch((error) =&gt; {
  11. console.log(error);
  12. });
  13. }, []);
  14. const [todo, dispatch] = useReducer(todoReducer, initialState || []);
  15. if (initialState === null) {
  16. return &lt;div&gt;Loading...&lt;/div&gt;;
  17. }
  18. return (
  19. &lt;TodoContext.Provider value={todo}&gt;
  20. &lt;TodoDispatchContext.Provider value={dispatch}&gt;
  21. {children}
  22. &lt;/TodoDispatchContext.Provider&gt;
  23. &lt;/TodoContext.Provider&gt;
  24. );
  25. }

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:

确定