亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 ESLint 響應 PropType

使用 ESLint 響應 PropType

慕娘9325324 2021-10-21 11:02:53
開始使用 ESLint 并遇到了 PropType 的幾個問題,目前我有一個道具children,現在假設這個道具是一個string. 我收到一個錯誤'children' is missing in props validationeslint(react/prop-types),為了解決這個問題,我只是PropTypes像這樣使用:CartProvider.propTypes = {  children: PropTypes.string,}以上解決了問題,然后我又遇到了另一個問題 propType "children" is not required, but has no corresponding defaultProps declaration.eslint(react/require-default-props)所以為了解決這個問題,我添加了另一個 PropType 像這樣:CartProvider.defaultProps = {  children: PropTypes.string,}所以我的理解是,每次我使用道具時,我都需要使用propTypeand defaultProps。好的,這解決了string基于道具的問題。但實際上這個道具是一個array然后array我得到了其他問題Prop typearray is forbiddeneslint(react/forbid-prop-types)。請幫助我理解為什么 ESLint 將這些道具標記為錯誤,我知道如何禁用它,但我想了解它為什么會發生,然后編寫更好的代碼。這是一個示例項目:購物車上下文import React from 'react'import PropTypes from 'prop-types'export const CartContext = React.createContext(null)export const CartProvider = ({ children }) => {  const [cartTotal, setCartTotal] = React.useState(0)  const incrementCartTotal = () => setCartTotal(cartTotal + 1)  return (    <CartContext.Provider      value={{        cartTotal,        incrementCartTotal,      }}    >      {children}    </CartContext.Provider>  )}CartProvider.propTypes = {  children: PropTypes.string,}CartProvider.defaultProps = {  children: PropTypes.string,}應用程序import React from 'react'import { CartContext, CartProvider } from './CartContext'function CartTotalItems() {  const { cartTotal } = React.useContext(CartContext)  return (    <p>      Total items currently in cart:      {cartTotal}    </p>  )}function AddToCart() {  const { incrementCartTotal } = React.useContext(CartContext)  return (    <button type="button" onClick={incrementCartTotal}>      Add to cart    </button>  )}export default function App() {  return (    <CartProvider>      <CartTotalItems />      <AddToCart />    </CartProvider>  )}更新 這對我有用,刪除 2 PropTypes 并添加以下內容:CartProvider.propTypes = {  children: PropTypes.node.isRequired,}
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

您誤解了 的類型children并混淆了使用defaultProps:


// Use for initial value

CartProvider.defaultProps = {

  counter: 10,


/* 

  You declared the initial value to be the value of `PropTypes.string`

  children: PropTypes.string

*/

}

// Children are always an Array of `ReactElement`/ `ReactElement` node.

CartProvider.propTypes = {

  counter: PropTypes.number,


  children: PropTypes.node.isRequired,


/* Same

  children: PropTypes.oneOfType([

        PropTypes.arrayOf(PropTypes.node),

        PropTypes.node

    ]).isRequired

*/


/*

  props.children can't be a string.

  children: PropTypes.string,

*/

}


查看完整回答
反對 回復 2021-10-21
?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

defaultProps 應該設置一個值而不是再次使用 Proptypes


CartProvider.propTypes = {

  children: PropTypes.string,

}


CartProvider.defaultProps = {

  children: "This is chidlren default string"

}


查看完整回答
反對 回復 2021-10-21
  • 2 回答
  • 0 關注
  • 432 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號