使用Redux的无状态组件

时间:2019-07-24 15:50:07

标签: reactjs redux components

我的问题是,如果我将redux与mapStateToProps一起使用,可以从父亲组件中获取道具。因为我试图将道具从父亲组件传递到我的无状态组件,而只是从mapStateToPros传递了道具。

这是我对组件的调用

case ModalsEnum.ListaTransferenciaDeCasos:
      return (
        <ListaTransferenciasDeCasos
        openModal={(modalCode) =>
          this.props.openModal(modalCode)}
          />
    );

这是我的无状态组件

import * as React from 'react';
import '../../../assets/css/notification.css'
import { connect }  from 'react-redux';
import { ModalsEnum } from '../../../entities/ModalsEnum';

const mapStateToProps = state => {
    return { transfereciasDeCasos: state.TransferenciaDeCasos.transferenciasDeCasos }
}
interface NotificationProps {
    transfereciasDeCasos:any,
    openModal: (modalCode: number) => void;
}
const Notification_ = ( props: NotificationProps ) => {
    const { transfereciasDeCasos, openModal } = props;
        return(
                    <button className="btn btn-default btn-notification" type="button" onClick={() => openModal(ModalsEnum.ListaTransferenciaDeCasos)}>
                        <img className="" src={require("../../../assets/icons/file-icons/svg/ic_notifications_24px.svg")} alt=""/> <span>{transfereciasDeCasos.length}</span>                          
                    </button>
        );
    }

const Notification = connect(mapStateToProps)(Notification_)

export default Notification

2 个答案:

答案 0 :(得分:0)

mapStateToProps还传递了ownProps。您可以像这样访问它们:

const mapStateToProps = (state, ownProps) => {
   return {
     transfereciasDeCasos: state.TransferenciaDeCasos.transferenciasDeCasos,
     ...ownProps
   }
}

答案 1 :(得分:0)

是的。您传递给子组件的道具将自动成为props对象的一部分。