我是新来的 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/