reactjs - 获取调度函数返回值

标签 reactjs react-redux redux-thunk

我是新来的 react 。我尝试将组件和 Action 函数分开。但我无法从单独的操作函数中获取返回值。是否可以从调度函数返回一个值(例如 Object {})

我把简短的代码如下:

LoginComponent.js

class Login extends React.Component {
   constructor(props){
      super(props)
      this.state = {
         username : '',
         password : ''
      }
   }
   submit = (e) => {
      /* console.logging "Some response"*/
      console.log(this.props.doLogin(this.state))
   }
   render(){
      return (
         <form onSubmit={this.submit}>/* some login element */</form>
      )
   }
}

export default connect(null, {LoginAction})(Login);

LoginAction.js

export function doLogin(state){
   return dispatch => {
      return axios.post('login', state).then(res =>{

      return "Some response";

    })
   }
}

但它不返回任何值

谢谢。

最佳答案

与上面的答案相反,你实际上可以从 thunk 中返回任何你想要的东西。 Redux-thunk 会传递它。

在您的情况下,您的 thunk 返回 Promise<string> ,这意味着在您的组件中 this.props.doLogin(this.state)还将评估为 Promise<string> .

因此,不要尝试记录 Promise ,而是尝试将该日志代码切换到 this.props.doLogin(this.state).then(result => console.log(result);

关于reactjs - 获取调度函数返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48294744/

相关文章:

javascript - 我可以在保持 onChange 功能的同时在 React 中将输入字段设置为只读吗?

React 应用程序刷新后 CSS 样式消失

javascript - 如何从 React 组件的 props 中的数组中获取项目并渲染到页面?

reactjs - 如何在reactjs中获取服务器发送的JWT cookie

javascript - Redux-Thunk - 异步 Action 创建者 promise 和链接不起作用

javascript - 如何将 Action 创建者绑定(bind)到 redux thunk 中的组件

reactjs - 通过 containerElement 属性将 NavLink 传递到 Material UI 组件会出现 “Failed prop type” 警告

javascript - 如何将提供者正确附加到reactDOM.render

javascript - 如何使用 Redux 将用户选择传输到另一个组件?

reactjs - 在react js中应用applyMiddleware(thunk)时获取 "Cannot call a class as a function"