首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >类型'IntrinsicAttributes & InferPropsInner‘上不存在属性'X’

类型'IntrinsicAttributes & InferPropsInner‘上不存在属性'X’
EN

Stack Overflow用户
提问于 2021-04-27 02:37:42
回答 2查看 56关注 0票数 0

在下面的代码中,我将.js文件重构为.tsx

代码语言:javascript
运行
AI代码解释
复制
import React, { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import { checkMobile } from '../../utils/common'
import { animateScroll as scroll } from 'react-scroll'

import ErrorBoundary from '../../components/error-boundary'
import Section1 from './section1'
import Section2 from './section2'
import Section3 from './section3'
import Section4 from './section4'
import Section5 from './section5'
import Section6 from './section6'
import Section7 from './section7'

export default function Main (props) {
  const {
    bannerData
  } = props

  const [currentPage, setCurrentPage] = useState(0)
  const [isMobile, setIsMobile] = useState(false)

  const mainScrollFunc = () => {
    document.querySelector('header').classList.add('has-bg')

    if (document.querySelector('.mainScrollEvent')) {
      const scrollPosition = (document.body.scrollTop || document.documentElement.scrollTop) - (window.innerHeight / 1.5)
      const mainSections = document.querySelectorAll('.main-section')
      console.log('mainSections[0]', mainSections[0])
      const paddingTop = 90
      let toIndex = 0

      if (scrollPosition <= (mainSections[0] as HTMLElement).offsetTop - paddingTop) {
        toIndex = 0
      } else if (scrollPosition <= (mainSections[1] as HTMLElement).offsetTop - paddingTop) {
        toIndex = 1
      } else if (scrollPosition <= (mainSections[2] as HTMLElement).offsetTop - paddingTop) {
        toIndex = 2
      } else if (scrollPosition <= (mainSections[3] as HTMLElement).offsetTop - paddingTop) {
        toIndex = 3
      } else if (scrollPosition <= (mainSections[4] as HTMLElement).offsetTop - paddingTop) {
        toIndex = 4
      } else if (scrollPosition <= (mainSections[5] as HTMLElement).offsetTop - paddingTop) {
        toIndex = 5
      } else if (scrollPosition <= (mainSections[6] as HTMLElement).offsetTop - paddingTop) {
        toIndex = 6
      }

      for (let i = 0; i < 7; i++) {
        document.querySelectorAll('.main_pager_button')[i].classList.remove('selected')
      }
      document.querySelector('.pagerButton' + toIndex).classList.add('selected')

      if (toIndex === 0) {
        if (document.querySelector('.top-banner')) {
          document.querySelector('.top-banner').classList.add('is-active')
          document.body.classList.add('has-banner')
        }
      } else {
        if (document.querySelector('.top-banner')) {
          document.querySelector('.top-banner').classList.remove('is-active')
          document.body.classList.remove('has-banner')
        }
      }

      setCurrentPage(toIndex)
    }
  }

  const onScroll = () => {
    const scrTop = document.body.scrollTop || document.documentElement.scrollTop
    const windowHeight = window.innerHeight
    const scrollPoint = windowHeight - 150
    if (scrollPoint < scrTop) {
      if (document.querySelector('.top-banner')) {
        document.querySelector('.top-banner').classList.remove('is-active')
        document.body.classList.remove('has-banner')
      }
    } else {
      if (document.querySelector('.top-banner')) {
        document.querySelector('.top-banner').classList.add('is-active')
        document.body.classList.add('has-banner')
      }
    }
    if (document.body.classList.contains('is-active')) {
      const bodyTop = document.body.style.top.replace('px', '')
      const mainSection1 = document.querySelector<HTMLElement>('.main-section.visual').offsetHeight - 150
      const header = document.querySelector('header')
      if (Math.abs(parseInt(bodyTop)) > mainSection1) {
        if (document.querySelector('.top-banner')) {
          document.querySelector('.top-banner').classList.remove('is-active')
          document.body.classList.remove('has-banner')
        }
        if (header.classList.contains('is-white')) {
          document.querySelector('header').classList.remove('is-white')
          document.querySelector('header').classList.add('has-bg')
          document.querySelector('header').classList.add('has-white')
        }
      }
    }
  }

  const toMove = (e) => {
    if (e.target.classList[0] === 'main_pager_button') {
      e.preventDefault()

      const href = e.target.getAttribute('href')
      const offsetTop = document.querySelector(href).offsetTop

      scroll.scrollTo(offsetTop, { duration: 500, smooth: 'easeInOut' })
    }
  }

  useEffect(() => {
    if (typeof window !== 'undefined') {
      setIsMobile(!!checkMobile(window.innerWidth))
      if (document.querySelector('.mainScrollEvent')) {
        if (checkMobile(window.innerWidth)) {
          window.addEventListener('scroll', onScroll)
        } else {
          window.addEventListener('scroll', mainScrollFunc)
        }
      }
    }
    return () => {
      if (checkMobile(window.innerWidth)) {
        window.removeEventListener('scroll', onScroll)
      }
    }
  }, [])

  return (
    <>
      <div id="contentsWrap" className="contents-wrap mainScrollEvent">
        <Section1 isMobile={isMobile} bannerData={bannerData} />
        <Section2 currentPage={currentPage}/>
        <Section3 />
        <Section4 isMobile={isMobile} currentPage={currentPage}/>
        <Section5 isMobile={isMobile} currentPage={currentPage} />
        <ErrorBoundary>
          <Section6 currentPage={currentPage} />
        </ErrorBoundary>
        <ErrorBoundary>
          <Section7 isMobile={isMobile} currentPage={currentPage}/>
        </ErrorBoundary>
        <nav className="main_pager_box" onClick={toMove}>
          <a href="#visual" className="main_pager_button pagerButton0 selected"></a>
          <a href="#since" className="main_pager_button pagerButton1"></a>
          <a href="#poomgo" className="main_pager_button pagerButton2"></a>
          <a href="#advantage" className="main_pager_button pagerButton3"></a>
          <a href="#difference" className="main_pager_button pagerButton4"></a>
          <a href="#cooperate" className="main_pager_button pagerButton5"></a>
          <a href="#contents" className="main_pager_button pagerButton6"></a>
        </nav>
      </div>
      <button type='button' className='btn btn-hidden' onClick={() => {
        scroll.scrollToTop({ duration: 500, smooth: 'easeInOut' })
      }}>TOP</button>
    </>
  )
}

Main.propTypes = {
  bannerData: PropTypes.array
}

我在一行中得到以下错误

代码语言:javascript
运行
AI代码解释
复制
<Section1 isMobile={isMobile} bannerData={bannerData} />

错误状态

代码语言:javascript
运行
AI代码解释
复制
Type '{ isMobile: boolean; bannerData: any; }' is not assignable to type 'IntrinsicAttributes & InferPropsInner<Pick<{ bannerData: Requireable<any[]>; }, never>> & Partial<InferPropsInner<Pick<{ bannerData: Requireable<any[]>; }, "bannerData">>>'.
  Property 'isMobile' does not exist on type 'IntrinsicAttributes & InferPropsInner<Pick<{ bannerData: Requireable<any[]>; }, never>> & Partial<InferPropsInner<Pick<{ bannerData: Requireable<any[]>; }, "bannerData">>>'.ts(2322)

我相信这是因为我以props的形式发送了我的isMobile状态。但是,我不明白为什么这个错误只发生在Section1上,而不是其他Section4Section5Section7上,因为它们的props中有isMobile

我曾尝试使用bannerDataisMobile添加到const中,但随后收到另一个错误,告诉我不能重新声明块范围内的变量isMobile。如何解决此错误?

*编辑*

按照要求,下面是我的.js文件形式的Section1代码

代码语言:javascript
运行
AI代码解释
复制
import React, { useState } from 'react'
import Image from 'next/image'
import PropTypes from 'prop-types'
import Slider from 'react-slick'
import useSWR from 'swr'
import { AxiosService } from '../../utils/axios-service'
import { getCloundFrontUrl } from '../../utils/common'

const axios = AxiosService.create()
const bannerUrl = '/banners?display=true&_sort=displayOrder:ASC'
export default function Section1 (props) {
  const { data: banners, error } = useSWR(bannerUrl, (url) => {
    return axios.get(url).then(res => res.data)
  }, {
    initialData: props.bannerData
  })
  const [currentSlide, setCurrentSlide] = useState(0)
  const settings = {
    arrows: true,
    infinite: true,
    autoplay: true,
    speed: 300,
    autoplaySpeed: 3600,
    slidesToShow: 1,
    slidesToScroll: 1,
    className: 'banner-list swiper-list',
    pauseOnHover: false,
    afterChange: (index) => {
      setCurrentSlide(index)
    }
  }
  if (!banners) {
    return <div>배너 읽는 중</div>
  }
  return (
    <div id="visual" className="main-section visual">
      <div className="main-section-content">
        {/* <!-- s: slider--> */}
        <div className="banner-list-wrap swiper-container">
          <Slider {...settings}>
            {
              banners.map(b => {
                const linkProps = {
                  ...(b.openNewTab && {
                    target: '_blank'
                  })
                }
                let imageUrl = ''
                if (b.images && b.images.length > 0) {
                  imageUrl = getCloundFrontUrl(b.images[0].url)
                }
                return (
                  <div className="banner-item swiper-item" key={b.id}>
                    <div className="img">
                      <Image className="img-drawer" src={imageUrl ? getCloundFrontUrl(imageUrl) : '/'} alt={b.images[0]?.alternativeText} layout="fill" />
                    </div>
                    <div className="visual-info">
                      <pre className="title">{b.title}</pre>
                      {b.description && (<pre className="desc">{b.description}</pre>)}
                      {b.linkUrl && (<a href={b.linkUrl} className="btn-go-detail" {...linkProps}>자세히보기</a>)}
                    </div>
                  </div>
                )
              })
            }
          </Slider>
          <div className="slider-assets">
            <div className="swiper-paging swiper-pagination-fraction">
              <span className="swiper-pagination-current">{currentSlide + 1}</span>
              <span className="swiper-pagination-slash">/</span>
              <span className="swiper-pagination-total">{banners.length}</span>
            </div>
          </div>
        </div>
        {/* <!-- e: slider--> */}
      </div>
    </div>
  )
}

Section1.propTypes = {
  bannerData: PropTypes.array
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-27 02:46:53

我是个笨蛋,Section1从来不用isMobile

票数 0
EN

Stack Overflow用户

发布于 2021-04-27 02:52:48

如果你想通过isMobile是Section1的道具,你需要更新Section1.propTypes

代码语言:javascript
运行
AI代码解释
复制
Section1.propTypes = {
  bannerData: PropTypes.array,
  isMobile: PropTypes.bool
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67276260

复制
相关文章
react native props上存在的属性,显示不存在
问题:类型“Readonly<{}> & Readonly<{ children?: ReactNode; }>”上不存在属性“navigation”。ts(2339) 解决方法: export d
windseek
2019/07/08
2.7K0
Spring Boot2.x系列教程(六)类型安全属性配置详解
在Spring中使用@Value可以对单个属性进行注入配置,但如果有很多配置属性或者配置属性本身拥有层级结构时,Spring Boot提供了基于类型安全的配置方式。本文系统的带大家了解一下基于类型安全的属性配置。
程序新视界
2019/12/30
7470
Intent 属性详解(上)
Android应用将会根据Intent来启动指定组件,至于到底启动哪个组件,则取决于Intent的各属性。本期将详细介绍Intent的各属性值,以及 Android如何根据不同属性值来启动相应的组件。 Intent 对象大致包含 Component、Action、Category、Data、Type、Extra 和 Flag 这 7 种属性,其中Component用于明确指定需要启动的目标组件,而Extra则用于携带需要交换的数据。 一、Component属性 Intent 的 Co
分享达人秀
2018/02/05
1.6K0
Intent 属性详解(上)
laravel 属性类型转换$casts
$casts 属性应是一个数组,且数组的键是那些需要被转换的属性名称,值则是你希望转换的数据类型。
友儿
2022/09/11
5410
Fundebug:JavaScript插件支持过滤特定属性不存在的错误
摘要: Fundebug的JavaScript错误监控插件更新至0.4.0,支持过滤特定属性不存在的错误。
Fundebug
2020/01/02
1K0
ios 获取属性的类型
如何在运行的时候动态获取到该属性的类型呢? 此方法获取属性的特性:property_copyAttributeValue unsigned int a; objc_property_t * result = class_copyPropertyList(object_getClass(k), &a); for (unsigned int i = 0; i < a; i++) { objc_property_t o_t = result[i]; NSLog(@"name:
onety码生
2018/11/21
2.8K0
C++0x 通用属性
C++在不断的发展,但每一阶段的C++标准提供的功能都很难完全满足现实需求,于是为了弥补标准的不足或者扩增特性应用场景所需的特性,各大C++编译器厂商多多少少在标准之外都增加了不少有用的扩展功能。这些扩展功能并不在C++的标准中,但是却经常被使用。有时候,C++标准委员会也会考虑这些标准之外的扩增特性,将其纳入标准之中。
恋喵大鲤鱼
2018/09/27
9220
如何通过反射获取属性的名字和属性类型
还是泛型dao中遇到的问题,以往我们要查询数据库中表中的数据的时候,需要每张表都会写一个dao操作数据库,现在的需求是只写一个dao,这是个万能的dao,适用于所有的表,进行增删改查都可用。显然我们事先不知道要查哪个表,泛型dao的基本要求就是对所有的表都适用,这就需要我们动态的获取表名,基本思想可以是方法中传入一个类(前提是数据库中的表和实体类都是一一对应的)的实例,通过反射获取这个实体类中的属性名和属性类型,这就用到了java中反射这个特性。
凯哥Java
2022/12/16
3.9K0
如何通过反射获取属性的名字和属性类型
Elasticsearch数据类型及其属性
dynamic和data_detection的详解:Elasticsearch dynamic mapping(动态映射) 策略.
双面人
2019/08/30
10.3K0
(六·)可选的对象属性类型
我们上一章节中定义的对象属性,使用的时候必须要包含定义的属性,否则就会报错,那么如果在我们开发中,有些属性不是必须的怎么办呢?
老怪兽
2023/02/22
1.4K0
为类型增加选择属性
using Microsoft.Practices.Prism.ViewModel; namespace Common { /// <summary> /// 增加选择属性 /// </summary> /// <typeparam name="T"></typeparam> public class SelectableObject<T> : NotificationObject { public SelectableObject(T it
用户6362579
2019/09/29
1.1K0
TS解决引入插件的类型文件不存在的问题
在我们使用TypeScript进行开发时,经常会使用到一些好久都没有维护但是又很流行的插件,这些插件基本都是JavaScript进行开发的在TypeScript项目中会报错。
用户6256742
2022/07/17
1.7K0
TS解决引入插件的类型文件不存在的问题
JavaScript 类型转换(上)
Number() 转换为数字, String() 转换为字符串, Boolean() 转换为布尔值。
陈不成i
2021/07/16
5730
获取对象属性类型、属性名称、属性值的研究:反射和JEXL解析引擎
先简单介绍下反射的概念:java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。 反射是java中一种强大的工具,能够使我们很方便的创建灵活的代码,这些代码可以在运行时装配。在实际的业务中,可能会动态根据属性去获取值。 工具类如下: package com.yaoguang.common.utils.field; import java.beans.Bean
猿人谷
2018/01/17
6.7K0
Entity Framework复杂类型属性映射
以上代码在ORM中称为组合类,EF会将这两个类映射在一张表中。当Code First发现不能推断出类的主键,并且没有通过Data Annotations或Fluent API注册主键,那么该类型将被自动注册为复杂类型。
喵叔
2020/09/08
7240
Entity Framework复杂类型属性映射
EF 通过DataAnnotations配置属性和类型
 一、通过Attribute配置约束 1、主键约束 通过KeyAttribute来配置主键约束,代码如下: [Key] public int PrimaryKey{ get; set; } 2、外键约束 通过ForeignKeyAttribute来配置外键约束,代码如下: [Key] public int PrimaryKey{ get; set; } [ForeignKey("ForeignKey")] public int PrimaryKey{ get; set; } 注意,指定列名存在(外键必须存在
郑小超.
2018/01/26
1.1K0
大战SQL列类型及其列属性
最近,在看一本《原则》的书籍,是写的一位美国人投资史。其中谈到和他的创业伙伴关系出现裂缝时,我们会怎样做?
小Bob来啦
2020/12/08
1.4K0
大战SQL列类型及其列属性
MessagePack Java 0.6.X 动态类型
我们知道 Java 是一个静态类型的语言。通过输入 Value MessagePack能够实现动态的特性。
HoneyMoose
2019/08/07
3600
点击加载更多

相似问题

类型“IntrinsicAttributes”上不存在属性“”store“”

2134

'IntrinsicAttributes‘类型上不存在属性'history’

16

“IntrinsicAttributes& Props &{IntrinsicAttributes?:ReactNode;}”类型上不存在属性“”Props“”

1189

“IntrinsicAttributes”类型上不存在模式属性“”show“”

1148

属性'ref‘在'IntrinsicAttributes’类型上不存在

12
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档