【问题标题】:Can't export action creator to component mapDispatchToProps method无法将动作创建者导出到组件 mapDispatchToProps 方法
【发布时间】:2021-01-18 03:24:21
【问题描述】:

在我的操作文件中,我创建了一个 fetchMethod

export const requestEmbedToken = () => (dispatch) => {

    return axios.get('http://localhost:7071/api/getTokens')
        .then(response => {
            console.log("token embed", response)

        })
}

app.js

从 './redux/actions' 导入 { requestEmbedToken }

function App(token, requestToken) {
  const [embedToken, setEmbedToken] = useState(token.token);
  useEffect(() => {
    if (token.token === undefined) {
      requestToken()
      setEmbedToken(token.token)
    }
  });
  return (

const mapDispatchToProps = (dispatch) => {
  return ({
    requestToken: () => dispatch(requestEmbedToken())
  })
}
export default connect(mapStateToProps, mapDispatchToProps)(App)

其实我遇到了一个错误

TypeError: requestToken 不是函数

【问题讨论】:

  • 我认为应该是props.requestToken(),我不确定您是否必须将mapDispatchToProps 的返回值括在括号中。

标签: javascript reactjs react-redux react-thunk


【解决方案1】:

App 组件将接收所有道具作为将作为参数传递的对象的属性,通常称为props 对象。您需要解构props 对象才能访问令牌和其他道具

function App({ token, requestToken }) {
  ...
}

或者,您可以使用 props 对象而不是对其进行解构

function App(props) {
  ...
}

然后使用props.tokenprops.requestToken 作为props 对象的属性访问props。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 2011-02-17
    相关资源
    最近更新 更多