gpt4 book ai didi

javascript - 类型 'IntrinsicAttributes'(自定义 Hook )上不存在 React 属性

转载 作者:行者123 更新时间:2023-12-02 21:47:53 25 4
gpt4 key购买 nike

我有一个自定义钩子(Hook)来显示模式,但是得到

Type '{ children: Element; modalOpen: boolean; onClose: () => void; }' is not assignable to type 'IntrinsicAttributes & { children: any; }'.
Property 'modalOpen' does not exist on type 'IntrinsicAttributes & { children: any; }'.

我的自定义 Hook :

import React, { useState, HTMLProps } from 'react';
import styled from 'styled-components';
import AriaModal from 'react-aria-modal';

type ModalWrapperProps = HTMLProps<HTMLDivElement> & {
large: boolean;
formModal?: boolean;
};

const ModalWrapper = styled(ContainerBoxShadow).attrs({ className: 'ModalWrapper' })`
${(_: ModalWrapperProps) => ''}
width: ${props => (props.formModal ? '80vw' : '90vw')};
max-height: 90vh;
overflow-x: hidden;
overflow-y: auto;

@media ${props => props.theme.devices.medium} {
width: ${props => (props.large ? '80vw' : '50vw')};
max-height: 80vh;
}

@media ${props => props.theme.devices.large} {
width: ${props => (props.large ? '80vw' : '40vw')};
}

@media ${props => props.theme.devices.laptop} {
width: ${props => (props.large ? '80vw' : '30vw')};
}
`;

const ModalCloseButton = styled(ButtonClosePrimary).attrs({ className: 'ModalCloseButton' })`
margin-bottom: 2rem;
float: right;
`;

const useModal = () => {
const [modalOpen, setModalOpen] = useState(false);

const ModalComponent = ({ children }, large, formModal) => {
return (
<AriaModal onExit={setModalOpen(false)} alert focusDialog titleId="modal-title" verticallyCenter>
<ModalWrapper large={large} formModal={formModal}>
<div>
<ModalCloseButton onClick={() => setModalOpen(false)}></ModalCloseButton>
</div>
{children}
</ModalWrapper>
</AriaModal>
);
};

return {
ModalComponent,
modalOpen,
setModalOpen,
};
};

export default useModal;

然后我在另一个文件(buttonPage)中调用它:

const ProfileDataPage = () => {
const { ModalComponent, modalOpen, setModalOpen } = useModal();

return (
<ButtonAccentPrimary tabIndex={0} onClick={() => setModalOpen(true)}>
some button text
</ButtonAccentPrimary>


<ModalComponent modalOpen={modalOpen} onClose={() => setModalOpen(false)}>
<myPage />
</ModalComponent>
);
};

预期行为:当我进入页面时,我们将其称为 ButtonPage,然后单击我的按钮,我的模式应该出现,并且我应该能够再次关闭它。

实际行为:当我进入 ButtonPage 时,模式已经打开,我无法关闭它。

我应该指出错误发生在 ModalComponent 上

最佳答案

const ModalComponent = ({ children }, large, formModal) => {

这里的问题似乎是 { Children } 包含您传递给 ModalComponent 的所有 Prop ,您可以尝试仅使用 Children Prop ,如下所示:

const ModalComponent = (children, large, formModal) => {

关于javascript - 类型 'IntrinsicAttributes'(自定义 Hook )上不存在 React 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60207646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com