getObjectByID(id)导致无限循环

时间:2019-05-21 11:33:14

标签: reactjs rest redux

我正在创建带有react和redux的应用程序,当我尝试通过API的id获取对象时,它不起作用,为了更好地理解代码,请参见:

操作:

export const getZoneById= (id_zone) => dispatch => {
    axios.get(`${API}${id_zone}`)
    .then(res=> {
        dispatch({
            type: GET_ZONEBYID,
            payload: res.data
        });   })
}

减速器:

case GET_ZONEBYID:
            return{
                ...state,
                zonebyid: action.payload
            };

zone.js

if效果很好,else效果不佳

componentDidMount(){

      this.props.getZone();
      this.props.getZoneById(id) //how can i get this id
    }


    renderZoneList(){
        return this.props.zones.map((zone) =>{

          if(zone instanceof Object)

          {

            return (

                <tr key={zone.id_zone}>
                      <td>{zone.id_zone}</td>
                      <td>{zone.zone}</td>
                      <td>{zone.description}</td>
                      <td>{zone.etat}</td>

                      </tr>              
            )}

          else { 
          //zone it becomes the id
          //how can i pass this id=zone to the componentDidMount
          const z=this.props.zonebyid;
            return (
              <tr key={z.id_zone}>
                    <td>{z.id_zone}</td>
                    <td>{z.zone}</td>
                    <td>{z.description}</td>
                    <td>{z.etat}</td>

              </tr>              
            )


          } 
        })
    }

0 个答案:

没有答案