简体   繁体   English

使用mapStateToProps连接到redux存储的确认模式会给出错误

[英]Confirmation modal connecting to redux store using mapStateToProps gives an ERROR

I have a modal component. 我有一个模态组件。 When the close button of the modal is clicked, I need to show up a confirmation modal. 单击模态的关闭按钮时,我需要显示确认模式。 It has yes and no options. 它有,没有选择。 This confirmataion component is connected to Redux store. 此确认组件连接到Redux商店。

Everything was working fine till I connect the confirmation component into the store using mapStateToProps . 一切正常,直到我使用mapStateToProps将确认组件连接到商店。 After that only I started getting this error. 在那之后,我才开始收到此错误。

Error: Could not find "store" in either the context or props of "Connect(_class)". 错误:无法在“Connect(_class)”的上下文或道具中找到“store”。 Either wrap the root component in a , or explicitly pass "store" as a prop to "Connect(_class)". 将根组件包装在a中,或者将“store”显式传递为“Connect(_class)”的prop。 at invariant (bundle.js:21701) at invariant(bundle.js:21701)

I have attached the image below. 我附上了下面的图片。

在此输入图像描述

Here's my confirmation modal. 这是我的确认模式。

import React, { Component, PropTypes } from 'react';
import AriaModal from 'react-aria-modal';
import { confirm } from '../util/confirm';
import { confirmable } from 'react-confirm';

class Confirmation extends Component {
  constructor(props) {
    super(props);
    this.getApplicationNode = this.getApplicationNode.bind(this);
  }

  getApplicationNode = () => {
    return document.getElementById('application');
  }

  render() {
    console.log("rendering confirmation....");
    const {
     okLabbel = 'OK',
     cancelLabel = 'Cancel',
     title,
     confirmation,
     show,
     proceed,
     dismiss,
     cancel,
     enableEscape = true,
   } = this.props;

    const modal =
        <AriaModal
          titleText="demo one"
          onExit={this.deactivateModal}
          mounted={this.props.modalActive}
          initialFocus="#demo-one-deactivate"
          getApplicationNode={this.getApplicationNode}
          underlayStyle={{ paddingTop: '2em' }}
        >
          <div id="test-modal" className="background">
            <div className='model-title'>
              <h3 className='pe-title'>{title}</h3>
            </div>
            <div className="modal-body">
              {confirmation}
            </div>
            <footer className="modal-footer">
              <button className="btn btn-info" onClick={cancel}>{cancelLabel}</button>
              <button className='btn btn-danger' onClick={proceed}>{okLabbel}</button>
            </footer>
          </div>
        </AriaModal>;

    return (
      <div>
        {modal}
      </div>
    );
  }

}

Confirmation.propTypes = {
  okLabbel: PropTypes.string,
  cancelLabel: PropTypes.string,
  title: PropTypes.string,
  confirmation: PropTypes.string,
  show: PropTypes.bool,
  proceed: PropTypes.func,     // called when ok button is clicked.
  cancel: PropTypes.func,      // called when cancel button is clicked.
  dismiss: PropTypes.func,     // called when backdrop is clicked or escaped.
  enableEscape: PropTypes.bool,
}

export default confirmable(Confirmation);

This is how I connected it to the store in a container component. 这就是我将它连接到容器组件中的商店的方式。

import { connect } from 'react-redux';
import Confirmation from '../components/Confirmation';

const mapStateToProps = (state) => {
  console.log("Calling mapStateToProps here!" + state.confirmation.modalActive);
  return { modalActive: state.confirmation.modalActive };
}

export default connect(mapStateToProps)(Confirmation);

You may see in the console the initial actioin on setting the visibility to true is working fine and the action is logged in the console like this. 您可能会在控制台中看到将可见性设置为true的初始操作正常工作,并且操作会像这样记录在控制台中。

{type: ON_CONFIRM, payload: true}

What's wrong with this mapStateToProps ? 这个mapStateToProps什么问题?

You may find the code for the project here . 您可以在此处找到项目的代码。

Why I am getting this error. 为什么我收到此错误。 What is going wrong here? 这里出了什么问题? Any help is appreciated. 任何帮助表示赞赏。

The problem is in the react-confirm library. 问题出在react-confirm库中。 It renders your confirmation model in it's own component tree and not in your application's component tree. 它将确认模型呈现在它自己的组件树中,而不是在应用程序的组件树中。 If you look at its source code , you can see that it use ReactDOM.render internally to create a new component tree. 如果查看其源代码 ,可以看到它ReactDOM.render内部使用ReactDOM.render来创建新的组件树。 It's like you have two React application on the same page. 这就像你在同一页面上有两个React应用程序。

mapStateToProps of react-redux use React context to access the store. mapStateToProps of react-redux使用React上下文来访问商店。 But as connected Confirmation component is rendered in a different component tree, connect HoC can't access the redux store via context. 但是,由于连接的确认组件在不同的组件树中呈现,因此连接HoC无法通过上下文访问redux存储。

The simplest solution would be finding another library which doesn't have this problem. 最简单的解决方案是找到另一个没有这个问题的库。 But alternatively, you can inject your context manually into your connected Confirmation using a HoC. 但是,您也可以使用HoC手动将context注入连接的确认中。

Here is a HoC which you can use to inject the context to a component: 这是一个HoC,您可以使用它将上下文注入组件:

function withContext(WrappedComponent, context){

  class ContextProvider extends React.Component {
    getChildContext() {
      return context;
    }

    render() {
      return <WrappedComponent {...this.props} />
    }
  }

  ContextProvider.childContextTypes = {};
  Object.keys(context).forEach(key => {
    ContextProvider.childContextTypes[key] = React.PropTypes.any.isRequired; 
  });

  return ContextProvider;
}

Let's assume that you render your connected Confirmation component in a component call Page. 假设您在组件调用页面中呈现已连接的确认组件。 Then you can inject the context using above HoC like this. 然后你可以使用上面的HoC注入上下文。

class Page extends React.Component {

  //......

  static contextTypes = {
    store: React.PropTypes.object.isRequired
  }

  render(){

    const ConfirmationWithContext = withContext(ConnectedConfirmation, this.context);

    return (
      //.....
      <ConfirmationWithContext/> // with your props
      //.....
    );
  }

}

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

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