简体   繁体   English

React Redux:道具更改时,从ACTION重新加载组件

[英]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.

相关问题 从 redux 动作更改道具后,反应 class 组件不重新渲染 - React class Component not re render after props change from redux action 更改道具价值,在场外反应并重新加载组件 - Change props value outsite react and reload component 如何等待 Redux 操作从 React 组件更改状态 - How to wait for a Redux action to change state from a React component 如何将组件 props 从 React 传递到 Redux? - how to pass component props from React to Redux? React/Redux - 何时使用状态和道具/何时更改道具 - React/Redux - When to use state and props / when to change props 如何从 redux 添加操作连接到组件道具 typescript - How to add action from redux connect to component props typescript 传递给道具的状态传播的变化停止在react-redux连接的组件上 - Change in state propagation to props stops at react-redux-connected component React-redux 组件未在商店道具更改时重新渲染 - React-redux component not re-rendering on store props change 异步 redux 获取数据的操作导致组件重新加载并导致对重新加载中的最大深度做出反应 - Async redux action to fetch data is causing component to reload and cause react to react max depth in reload 从链接调用反应组件并传递道具时,页面重新加载失败 - Page reload fails when calling a react component from the Link and passing the props
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM