所以我有一個要加載并存盤在陣列中的組件串列
例如 array = [<component1/>,<component2/>,<component3/>,]
所以這是我的代碼以及我如何構建一切作業
宣告:
let components = []
將組件加載到陣列中:
useEffect(() => {
for(let i=0;i<length;i ){
components[i] = <RadioButtonRN
data={roadsigns.signs_questions[i].answers}
box={false}
textColor={primary}
initial={2}
selectedBtn={(e) => selectedAnswer(e)}
/>
}
connsole.log(components)
},[])
回傳顯示組件:
return (
<div>
{components}
</div>
);
};
不知何故,這段代碼確實將組件加載到陣列中,但它不顯示任何內容。
uj5u.com熱心網友回復:
嘗試這個
import React from "react";
function App() {
const compArray = () => {
let arr = [];
for (let i = 0; i < 3; i ) {
arr.push(<Name />);
}
return arr;
};
return <>{compArray()}</>;
}
export default App;
function Name() {
return <h1>Hello world </h1>;
}
uj5u.com熱心網友回復:
您需要key為每個組件添加一個。我相當肯定它應該在你這樣做后作業(假設}你之后的尾隨return是函式的結尾。
對于一個簡單的key任務:
components[i] = <RadioButtonRN
key={i}
data={roadsigns.signs_questions[i].answers}
...
/>
uj5u.com熱心網友回復:
如果你想要相同組件的陣列,
return < > {
components.map((el, i) => < Yourcomponent key = {
i
}
otherParams = {
el
}
/>)} <
/>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489735.html
標籤:javascript 数组 反应 反应式
