【问题标题】:Curried function causing error but works if not curried咖喱函数导致错误,但如果不咖喱则有效
【发布时间】:2020-02-15 09:54:34
【问题描述】:
export const postMoviePopular = url = dispatch => {
    const data = axios.get(url);
    dispatch(saveMoviePopular(data));
}

const saveMoviePopular = payload => {
    return {
        type: POST_MOVIE_POPULAR,
        payload
    }
}

这是我的代码,因为它被咖喱而不工作,但如果它不是像下面这样的咖喱,它可以工作,为什么会这样??

export const postMoviePopular = url => {
    return dispatch => {
        const data = axios.get(url);
        dispatch(saveMoviePopular(data));
    }    
} 

我想知道这是否与我调用 mapDispatchToProps 的方式有关?

componentDidMount() {
        this.props.postMDBConfig(`https://api.themoviedb.org/3/configuration?api_key=${this.props.apiKey}`);
        this.props.postMoviePopular(`https://api.themoviedb.org/3/movie/popular?api_key=${this.props.apiKey}&language=en-US&page=1&region=US`)
    }

const mapDispatchToProps = (dispatch) => {
    return {
        postMDBConfig: url => dispatch(postMDBConfig(url)),
        postMoviePopular: url => dispatch(postMoviePopular(url))
    }
}

【问题讨论】:

  • 我认为你的第一个版本在第一行有 = 你真正想要的 =>
  • @Pointy,但这会引发错误。
  • 我的意思是第二个=;没有它,url 只是一个变量。

标签: javascript reactjs api redux


【解决方案1】:

您需要一个返回语句。还有一个箭头。

export const postMoviePopular = url => dispatch => {
    const data = axios.get(url);
    return dispatch(saveMoviePopular(data));
}

【讨论】:

  • 也不应该是url => dispatch =>吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 2011-04-21
  • 2013-08-26
相关资源
最近更新 更多