简体   繁体   English

React / Redux,在路由中重新加载组件

[英]React/Redux, Reloading a component in route

I have the typical list of rows in a basic CRUD screen, each row, as usual with the link: 我在基本的CRUD屏幕中有典型的行列表,每行与往常一样带有链接:

<Link to={"/appointment/"+appointment.id+"/"}>Edit</Link>

My route is: 我的路线是:

<Route path="/appointment/:id" component={AppoModal} />

when I click "Edit" in any row a Modal dialogue appears: 当我在任何行中单击“编辑”时,将出现模式对话框:

在此处输入图片说明

If I do click in the first "Edit" link all works fine. 如果我确实单击第一个“编辑”链接,则一切正常。 But if I push the "Close" button in the dialogue and try to click any "Edit" link again, the modal dialogue is not launched, I guess this is happening because the component is already "up". 但是,如果我按下对话框中的“关闭”按钮,然后再次尝试单击任何“编辑”链接,则不会启动模式对话框,我猜这是由于组件已经“启动”而发生的。

The hide/show behaviour of the dialogue is controlled by this.state.showModal value in the AppoModal component: 对话的隐藏/显示行为是由AppoModal组件this.state.showModal值控制:

 constructor(props) {
    super(props);
    this.state = { showModal: true };
 }  

So I don't know how to "reload" or "re run" the component. 因此,我不知道如何“重新加载”或“重新运行”组件。 Can I run a dispatch(action) every time I click in the "Edit" link? 我每次单击“编辑”链接时都可以运行调度(操作)吗? I heard about a "static method", but I'm too newbie with React to know if that is the path. 我听说过“静态方法”,但是我对React还是一个新手,不知道这是否是路径。

Thx! 谢谢!

The problem arises because when you click Close, you're changing the component state, but you're not changing the application state. 出现问题是因为单击“关闭”时,您正在更改组件状态,但没有更改应用程序状态。

Since your modal opens with a route change, it should also close with a route change. 由于您的模式随路径更改而打开,因此它也应随路径更改而关闭。

You could take a different approach and avoid the route change all together. 您可以采用其他方法,避免一起改变路线。 Since you are using redux, you could have a global state which could contain a modal name as a constant or maybe contain the reference to the Component. 由于您使用的是redux,因此您可能具有一个全局状态,该状态可以包含模式名称作为常量,也可以包含对Component的引用。

Now you can have a modal component that would render the component depending on the global state change and you can call this component somewhere in the root Component. 现在,您可以有一个模态组件,该组件将根据全局状态更改来呈现该组件,并且可以在根组件的某个位置调用该组件。

so your reducer looks like 所以你的减速器看起来像

export function modalState(state=null, action) {
  if(action.payload.name == "CLOSE_MODAL") return null;
  else if([/*modal names*/].includes(action.payload.name) {
    return {modal: action.payload.name, .data: action.payload.data}
  } else return {...state}
}

and you have an action like 你有一个像

export function openModal(name, data) {
  return {
    type: "MODAL_NAME",
    payload: { name, data }
}

export function closeModal() {
  return { type: "CLOSE_MODAL", payoad: null }
}

and your component could look like 您的组件可能看起来像

const componentMaps = {
  [MODAL_1] : import MODAL_1 from "./modals/Modal_1.jsx"
}
cont Modal = React.createClass({
  render: function() {
    let Component = componentMaps[this.props.modal.name]
    if(Component) {
      return <Component {...this.props.modal.data}/>
    } else { 
      return null;
    }
  }
});

export connect(select)(Modal);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM