我正在使用 Reactjs。我想渲染一個(gè)存在于對(duì)象內(nèi)部的數(shù)組。state = { obj: { name: abhishek, age: 23, characterList: ['a','b','c','d'] }}我發(fā)現(xiàn)為了呈現(xiàn) characterList,我必須將其包裝在條件語(yǔ)句中。而且我無(wú)法弄清楚原因。不正確:render(){return( <> { this.state.obj.characterList.map((character, i) => { return( <h1>{character}</h1> ) }) } </> )這會(huì)產(chǎn)生錯(cuò)誤 - “無(wú)法讀取未定義的屬性‘map’”但是如果在這樣的條件語(yǔ)句中給出 -正確的:render(){return( <> { this.state.obj.characterList ? ( this.state.obj.characterList.map((character, i) => { return( <h1>{character}</h1> ) }) ) : null } </> )原始代碼:給出錯(cuò)誤(無(wú)法讀取未定義的屬性“映射”)export default (props) => { const [FAQs, setFAQs] = useState([]); useEffect(async () => { const res = await Axios.get("http://localhost:5000/FAQs/getFAQs"); setFAQs(res.data); console.log(res.data); }, []); return ( <React.Fragment> <div className="faqs-wapper"> <div className="faqs-heading"> <h1>Frequently asked questions</h1> <h2>{FAQs.category}</h2> //This works fine. </div> { FAQs.FAQList.map((QA,i) => { return ( <div key={i}> <p>{QA.question}</p> </div> ) }) } </div> </React.Fragment> );};
映射存在于對(duì)象內(nèi)部的數(shù)組僅在條件語(yǔ)句中給出時(shí)有效
ITMISS
2022-12-29 16:47:23