我想使用Axios返回全球状态,但请保证{< pending> }

发布于 2025-01-17 15:24:55 字数 1520 浏览 2 评论 0原文

const Board = () => {

    ...

    const {selected} = useSelector(state => state.board);
    // In Redux reducer ->
    // const initialState = {
    // selected : {}
    // }

    const getOnePost = async (id) => {
        try {
          const response = await axios.get(`/api/v1/post/${id}`);
          const getOnePostData = await response.data;
          selected = getOnePostData //I want to use Redux to use global state...
          console.log(selected) //TypeError... and How to use global state..?
        } catch(error) {
          alert(error.response.data.message);
          return Promise.reject(error)
        }
      }

    const postClickHandler = (postId) =>
    {
        if(postId) {
            // dispatch(boardSelected(postId));
            getOnePost(postId)
        }
    }

    ...

}

此代码使用 axios.get 接收帖子信息。

我想使用api返回全局状态(Redux状态)。

    const getOnePost = async (id) => {
        try {
          const response = await axios.get(`/api/v1/post/${id}`);
          const getOnePostData = await response.data;
          return getOnePostData //return data
        } catch(error) {
          alert(error.response.data.message);
          return Promise.reject(error)
        }
      }

    const postClickHandler = (postId) =>
    {
        if(postId) {
            getOnePost(postId).then((response)=>{
                return{...selected, selected: response} //But Seleted: {}
            })
        }
    }
const Board = () => {

    ...

    const {selected} = useSelector(state => state.board);
    // In Redux reducer ->
    // const initialState = {
    // selected : {}
    // }

    const getOnePost = async (id) => {
        try {
          const response = await axios.get(`/api/v1/post/${id}`);
          const getOnePostData = await response.data;
          selected = getOnePostData //I want to use Redux to use global state...
          console.log(selected) //TypeError... and How to use global state..?
        } catch(error) {
          alert(error.response.data.message);
          return Promise.reject(error)
        }
      }

    const postClickHandler = (postId) =>
    {
        if(postId) {
            // dispatch(boardSelected(postId));
            getOnePost(postId)
        }
    }

    ...

}

This code uses axios.get to receive post information.

and I want to use api's return to global state(Redux state).

    const getOnePost = async (id) => {
        try {
          const response = await axios.get(`/api/v1/post/${id}`);
          const getOnePostData = await response.data;
          return getOnePostData //return data
        } catch(error) {
          alert(error.response.data.message);
          return Promise.reject(error)
        }
      }

    const postClickHandler = (postId) =>
    {
        if(postId) {
            getOnePost(postId).then((response)=>{
                return{...selected, selected: response} //But Seleted: {}
            })
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江心雾 2025-01-24 15:24:55

axios是一个基于Promised的JavaScript库,用于发送HTTP请求,
你必须在 then 方法中得到承诺结果。
试试这个

const Board = () => {
...

const {selected} = useSelector(state => state.board);
// In Redux reducer ->
// const initialState = {
// selected : {}
// }

const getOnePost = async (id) => {
    try {
      const response = await axios.get(`/api/v1/post/${id}`);
      response.then(function (rec) {
            selected = rec.data //
            //here update you state 
            console.log(selected)
        }).catch(function (error) {
            console.log(error);
        }); 
    } catch(error) {
      alert(error.response.data.message);
      return Promise.reject(error)
    }
  }

const postClickHandler = (postId) =>
{
    if(postId) {
        // dispatch(boardSelected(postId));
        getOnePost(postId)
    }
}

...

}

Axios is a Promised-based JavaScript library that is used to send HTTP requests,
you have to get the promise result in the then method .
try this

const Board = () => {
...

const {selected} = useSelector(state => state.board);
// In Redux reducer ->
// const initialState = {
// selected : {}
// }

const getOnePost = async (id) => {
    try {
      const response = await axios.get(`/api/v1/post/${id}`);
      response.then(function (rec) {
            selected = rec.data //
            //here update you state 
            console.log(selected)
        }).catch(function (error) {
            console.log(error);
        }); 
    } catch(error) {
      alert(error.response.data.message);
      return Promise.reject(error)
    }
  }

const postClickHandler = (postId) =>
{
    if(postId) {
        // dispatch(boardSelected(postId));
        getOnePost(postId)
    }
}

...

}

时间你老了 2025-01-24 15:24:55

您可以使用 .then( )

尝试像下面这样初始化选定的内容:

const response = await axios.get(`/api/v1/post/${id}`).then( response => {
 selected = response
})

我不知道它是什么类型的数据。因此,尝试记录响应数据并使用它。

You can handle promise by using .then()

Try to initialize selected like below :

const response = await axios.get(`/api/v1/post/${id}`).then( response => {
 selected = response
})

I don't know what kind of data is it. So, try to log response data, and use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文