简体   繁体   English

REact/redux 应用程序 api 调用不起作用:显示空且 aciton 未运行问题

[英]REact/redux app api call not working : showing empty and aciton not running issue

hi guys I am creating a react native app having some problem when calling api get request its like the response giving some error seems like action not running ill share code below please if anyone know the issue please let me know嗨,伙计们,我正在创建一个反应原生应用程序,在调用 api 获取请求时遇到一些问题,就像给出一些错误的响应一样,下面的操作没有运行错误共享代码请如果有人知道这个问题,请告诉我

NOTE -- when I run the app console in reducer and console in action are not running aslo注意——当我在 reducer 中运行应用程序控制台时,运行中的控制台并没有运行


error message in console DetailsReducer reducer value in component when i do mapstatetoprops and check the value of reducer its showing like below当我执行 mapstatetoprops 并检查 reducer 的值时,控制台 DetailsReducer 减速器值在组件中出现错误消息,如下所示

Object { details: {}, isFetching: false, error: false }
details: Object {  }
<prototype>: Object { … }
error: false
isFetching: false
<prototype>: Object { … }

action file动作文件

export function fetchdetails(seesionkey) {
  return function (dispatch, getState) {
    fetch(`api url`, {
        method: 'GET',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json',
          seesionkey,
        },
        body: JSON.stringify({
          "appcont": getState().mustdata,
          "dataset":{}
        }),
      })
      .then(r => r.json())
      .then((rr) => {
        console.log('------testinresponse',rr);
        if (rr.response.error.errorCode === -4) {
          dispatch({
            type: 'FETCHFAIL',
            payload: ' error  again',
          });
        } else {
          dispatch({
            type: 'FETCHSUCCESS',
            payload: rr,
          });
        }

  })
}
}

reducer减速器

const initialState = {
    details: {},
    isFetching: false,
    error: false
}
export default function DetailsReducer(state = initialState, action) {

    switch(action.type) {
        case :FETCHSUCCESS
            console.log('action goes here', action)
            return {
                ...state,
                isFetching: false,
                details: action.payload
            }
        case FETCHFAIL:
            return {
                ...state,
                isFetching: false,
                error: true
            }
        default:
            return state
    }
}

component call组件调用


componentDidMount() {
 const {seesionkey} = this.props
 this.props.dispatch(fetchdetails(seesionkey));

  }

mock api模拟 api

{   
    "appcont":{
      "id":"34435654605",
      "name":"no data ",
      "details":dummy details",
      "code":"demo",
      
   },
   "dataset":{
        
   }
}

Without seeing the full component definition I cant 100% say for sure, but are you checking that the details is not empty before you log it out?如果没有看到完整的组件定义,我不能 100% 肯定地说,但是您是否在注销之前检查详细信息是否为空? When the component first mounts, it will be empty, until the request comes back -- when a second render will occur.当组件第一次挂载时,它将是空的,直到请求返回——此时将发生第二次渲染。

Typically, in the component render you would render nothing until it comes back.通常,在组件渲染中,您不会渲染任何内容,直到它返回。 Its up to you to handle the case where the network request is still in flight:由您来处理网络请求仍在进行中的情况:

render() {
   if (Object.keys(this.props.details).length === 0) return null

   // rest of component
}

It would probably be better to use isFetching in the condition but you will need another action just before the fetch call to set this.在条件中使用isFetching可能会更好,但您需要在 fetch 调用之前执行另一个操作来设置它。

Probably the reason the action doesnt even fire is your render already blew up because it couldnt handle the empty case.动作甚至没有触发的原因可能是您的渲染已经炸毁,因为它无法处理空箱。

Another problem could be if you have not configured https://github.com/reduxjs/redux-thunk on your store.另一个问题可能是您尚未在商店中配置https://github.com/reduxjs/redux-thunk You are using thunks, so you might see what you're seeing if you didnt configure them.您正在使用 thunk,因此如果您没有配置它们,您可能会看到所看到的内容。

Btw, off topic, but you probably want the dispatch in componentWillMount and not componentDidMount .顺便说一句,题外话,但您可能希望在componentWillMount而不是componentDidMount中调度。 Did mount will unnecessarily wait for the first render pass before launching the request.在启动请求之前, mount 会不必要地等待第一个渲染过程。 It'll be fractionally quicker in will mount.安装起来会快一点。

got the solution data was empty because the api request was get method and passing some data, then changed to post method and all working fine得到解决方案数据为空,因为 api 请求是获取方法并传递一些数据,然后更改为发布方法并且一切正常

i have figure out that your case syntax is wrong it should be like this inside reducer switch我发现你的 case 语法是错误的,在减速器开关里面应该是这样的

case "FETCHSUCCESS":案例“FETCHSUCCESS”:

second you have to warp your component with connect and then get the state from your reducer.其次,您必须使用connect扭曲组件,然后从减速器中获取 state。

to view you result you need to directly interact with reducer for data rather than using data from component state.要查看结果,您需要直接与 reducer 进行数据交互,而不是使用来自组件 state 的数据。

and use show loader inside component until API calls end并在组件内使用 show loader 直到API调用结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM