【问题标题】:Called the same javascript array function and behaving is different调用相同的javascript数组函数并且行为不同
【发布时间】:2018-12-15 22:03:54
【问题描述】:

我尝试为 React 应用编写 2 个操作。

export const changeFilter = (newFilter) => {

return (dispatch) => {
    dispatch({type:types.GET_EVENTS_BY_FILTER});
    axios.get( 'http://localhost:8762/event/events?') // + eventsSelector.getEventsFilter()
        .then( response => {
            dispatch({type:types.GET_EVENTS_BY_FILTER_SUCCESS,payload:response});
        } )
        .catch( error => {
            dispatch({type:types.GET_EVENTS_BY_FILTER_FAIL,error:error});
        } );


  }
};

export const getEventsByFilter = () => {

return (dispatch) => {
    dispatch({type:types.GET_EVENTS_BY_FILTER});
    axios.get( 'http://localhost:8762/event/events?') // + eventsSelector.getEventsFilter()
        .then( response => {
            dispatch({type:types.GET_EVENTS_BY_FILTER_SUCCESS,payload:response});
        } )
        .catch( error => {
            dispatch({type:types.GET_EVENTS_BY_FILTER_FAIL,error:error});
        } );
    }
};

当我点击 getEventsByFilter 中的 return(dispatch) 行并按 F10 进行调试(在 Chrome 开发人员工具中)时,我转到下一行(这是预期的行为)。不幸的是,当我尝试调用 changeFilter 方法并点击 line return(dispatch) 并点击 F10 转到下一行时,调试器将我引导到下一行的 return 语句的封闭括号。我还检查了日志,因为我安装了 redux 记录器,并且仅为 getEventsByFilter 方法调度了操作。

【问题讨论】:

    标签: javascript reactjs redux react-redux


    【解决方案1】:

    我对这些方法的称呼不同。它们都在 eventActions 文件中声明,我在调用这个函数的组件中有这个:

    const mapDispatchToProps = dispatch => {
        return {
            getEvents: () => dispatch(eventActions.getEvents()),
            getEventsByFilter: () => dispatch(eventActions.getEventsByFilter()),
            changeFilter: (newFilter) => dispatch(eventActions.changeFilter(newFilter))
        }
    };
    

    对于 getEventsByFilter,我使用了 this.props.getEventsByFilter();,而 changeFilter 被称为 eventActions.changeFilter({priceFrom:event.target.value});。我错过了dispatchchangeFilter 方法。

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2021-08-10
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多