[英]React Redux: Reload the component from ACTION when the props change
This is my component's class: 这是我组件的类:
import React, { Component } from 'react';
import {connect} from 'react-redux';
import Button from '../UI/Button/Button';
import * as actions from '../../store/actions';
class Password extends Component {
submitPassword=(e)=>{
e.preventDefault();
this.props.submitPassword(this.state.password, this.props.levelNumber);
}
render() {
<Button clicked={this.submitPassword} >
Submit password
</Button>
}
}
const mapStateToProps = state => {
return {
};
}
const mapDispatchToProps = dispatch => {
return {
submitPassword: (password,levelNumber) => dispatch(actions.submitPassword(password,levelNumber))
};
}
export default connect(mapStateToProps, mapDispatchToProps)(Password);
and this is my action: 这是我的动作:
export const submitPassword = () => {
// HERE ALL MY LOGIC !!!
return {
level:undefined,
type:actions.PASSWORD
}
}
The code working all correctly including params and logic. 该代码可以正常工作,包括参数和逻辑。
I wanna that every time that finish to execute the function submitPassword A THIRD COMPONENT refresh/reload with the new props. 我想每次完成执行功能SubmitPassword的第三部分时, 便会使用新道具刷新/重新加载。
It's possibile to send a command from action to component? 是否可以将命令从操作发送到组件? How can I do it?
我该怎么做?
I have already tried with: 我已经尝试过:
componentWillReceiveProps() {
console.log("new props");
}
in my component but he can not take the event. 在我的组件中,但他不能参加该事件。
If your component has props it will rerender when a prop change. 如果您的组件具有道具,它将在道具更改时重新呈现。 If you want a page reload (location.reload()) you are going to need to add a Middleware (like redux sagas) in order to handle that.
如果您想重新加载页面(location.reload()),则需要添加一个中间件(例如redux sagas)来进行处理。 This is because actions are asynchronous.
这是因为动作是异步的。
To be able to act on updated props your component will have to "watch" the props thats being changed. 为了能够对更新的道具采取行动,您的组件将必须“观察”正在更改的道具。 Without seeing your redux store I don't know what you want to accomplish but you should be able to add it to your:
没有看到您的redux存储,我不知道您想完成什么,但是您应该可以将其添加到您的:
const mapStateToProps = state => {
return {
desiredProp: state.desiredProp
};
}
and then in your class you can reach this by console.log(this.props)
assuming your redux is setup correctly. 然后在您的课程中,假设您的redux设置正确,您可以通过
console.log(this.props)
达到此console.log(this.props)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.