大佬教程收集整理的这篇文章主要介绍了ReactJS - PropTypes 未验证,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
PropTypes
不是验证(忽略!!!)这个简单的组件,我尝试了所有方法但它不起作用。
import React,{ useEffect,useState } from "react"; import PropTypes from "prop-types"; import Item from "./Item"; import chunkArray from "../../utils/chunkArray"; const Allitems = (props) => { const { items,title } = props; const [arrayToMap,setArrayToMap] = useState([]); // set size array based on columns const arrSize = 12; // chunk array useEffect(() => { setArrayToMap(chunkArray(items,arrSizE)); },[items]); return ( <> {arrayToMap.length === 0 ? ( <div>Wait</div> ) : ( <> {titlE} {arrayToMap.map((X) => { return ( <div classname="row"> {x.map((y,i) => ( <div classname="col-1" key={i}> <Item item={y} size={"small"} /> </div> ))} </div> ); })} </> )} </> ); }; Allitems.propTypes = { items: PropTypes.array.isrequired,title: PropTypes.String.isrequired,}; export default Allitems;
@H_403_7@我可以将任何内容传递给
ìtems
或title
,但不会停止渲染。有什么想法吗?
谢谢!
解决方法
重新渲染您的组件与属性类型无关,这是因为您在 useEffect 函数中设置了状态而没有任何条件。所以组件将在无限循环渲染中工作。你可以阅读它here
你需要添加一些条件来设置你的状态,就像这样:
conn = conn |> init_test_session(conn,%{token: "some-token"}) |> get("/some-request")
因此,如果您的 state 是 clear 并且您的
,useEffect(() => { if(arrayToMap.length === 0 && items.length > 0){ setArrayToMap(chunkArray(items,arrSizE)); } },[items]);
数组不为空,则条件将起作用。您没有看到警告的原因是因为您在组件中使用 props 作为参数。将您的组件更改为以下内容
const AllItems = ({ items,title }) => {}
Proptypes 不会阻止组件渲染,但您可以使用 eslint 来验证参数并阻止应用程序启动。将 eslintrc.json 文件添加到您的项目中并添加以下规则。
"react/prop-types": 2,
以上是大佬教程为你收集整理的ReactJS - PropTypes 未验证全部内容,希望文章能够帮你解决ReactJS - PropTypes 未验证所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。