首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换为类型记录:传递映射的道具

转换为类型记录:传递映射的道具
EN

Stack Overflow用户
提问于 2022-03-08 03:16:50
回答 2查看 116关注 0票数 -2

我很难将下面的React.JS脚本转换为TypeScript。有人能帮忙吗?我正试着在我的网站上放一个导航条。

这是我的Header.tsx文件:

我在onClick={closeMobileMenu}上得到一条红色行-- 'IntrinsicAttributes &{ items: any;}‘类型中不存在属性'onClick’。

代码语言:javascript
复制
<ul className="navbar-nav">
    {menuItems.map((menu, index) => {
    return (
      <MenuItems 
        items={menu}
        key={index}
        onClick={closeMobileMenu}
      />
    );
  })}
</ul>

这是我的Menu.tsx文件

我发现了一个错误

  1. "items":绑定元素'items‘隐式具有'any’
  2. 'contains‘在类型'never'

上不存在

代码语言:javascript
复制
import React, { useState, useEffect, useRef } from "react";
import {HashLink} from "react-router-hash-link";

import Dropdown from "./Dropdown";
import "./Header.css";

interface MenuItems {
  items: string
  key: number
  onClick: (param: any) => void
} 


const MenuItems = ({ items }) => {
  let ref = useRef();
  const [dropdown, setDropdown] = useState(false);
  const onMouseEnter = () => {
    window.innerWidth > 960 && setDropdown(true);
  };

  const onMouseLeave = () => {
    window.innerWidth > 960 && setDropdown(false);
  };

  useEffect(() => {
    const handler = (event: { target: any; }) => {
      if (dropdown && ref.current && !ref.current.contains(event.target)) {
        setDropdown(false);
      }
    };
    document.addEventListener("mousedown", handler);
    document.addEventListener("touchstart", handler);
    return () => {
      // Cleanup the event listener
      document.removeEventListener("mousedown", handler);
      document.removeEventListener("touchstart", handler);
    };
  }, [dropdown]);

  return (
    <li
      className="nav-item"
      ref={ref}
      onMouseEnter={onMouseEnter}
      onMouseLeave={onMouseLeave}
      onClick={() => setDropdown(false)}
    >
      {items.submenu ? (
        <>
          <button
            type="button"
            aria-haspopup="menu"
            aria-expanded={dropdown ? "true" : "false"}
          >
            <HashLink smooth to={items.path} className="nav-link">
              {items.title} <i className="fas fa-chevron-down"></i>
            </HashLink>
          </button>
          <Dropdown submenus={items.submenu} dropdown={dropdown} />
        </>
      ) : (
        <HashLink
          smooth to={items.path}
          className="first-level-nav-link"
        >
          {items.title}
        </HashLink>
      )}
    </li>
  );
};

export default MenuItems;

这是我的menuItems.tsx文件:

代码语言:javascript
复制
export const menuItems = [
  {
    title: "Home",
    path: "/",
    cName: "nav-link",
    submenu: [
      {
        title: "Story",
        path: "/#story",
        cName: "nav-link",
      },
      {
        title: "Map",
        path: "/#map",
        cName: "nav-link",
      }
    ],
  },
  {
    title: "Rewards",
    path: "/",
    cName: "nav-link",
    submenu: [
      {
        title: "competition",
        path: "competition",
        cName: "nav-link",
      },
      {
        title: "prizes",
        path: "prizes",
        cName: "nav-link",
      }
    ],
  },
  {
    title: "Downloads",
    path: "downloads",
    cName: "nav-link",
  }
];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-08 05:44:05

用于错误#2

如果没有任何额外的信息,TypeScript实际上无法推断您打算如何使用这个引用。

代码语言:javascript
复制
const ref = useRef() // React.MutableRefObject<undefined>

但是,useRef可以作为一个泛型来告诉TypeScript您打算如何使用ref。

代码语言:javascript
复制
const ref = useRef<HTMLLIElement>(null) // React.MutableRefObject<HTMLLIElement>

只有这样,TypeScript才允许您访问ref.current.contains,因为它知道contains属性存在于HTMLLIElement节点上。

票数 0
EN

Stack Overflow用户

发布于 2022-03-08 05:33:15

更正#1:对于函数引用作为参数,您需要定义任何数据类型

代码语言:javascript
复制
interface MenuItems {
  items: any,
  key: number,
  onClick: any
} 

更正#2:在您的MenuItems组件。

代码语言:javascript
复制
const MenuItems = (props: MenuItems ) => {

   //access menu item 
  console.log(props.items.title);

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71389653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档