React native 为什么我会出错:react native中的重播程序太多
我试图将数组传递给另一个组件。数据是一个json对象数组,其中包含数据。我知道数据在那里,因为当我单击按钮时,会调用handleclick方法并显示数据,但当我尝试将数据作为数组传递(季节显示和季节长度)时,我得到一个错误:错误:重新渲染过多。React限制渲染的数量以防止无限循环。如何将数据作为数组从fetch传递到下拉框组件React native 为什么我会出错:react native中的重播程序太多,react-native,React Native,我试图将数组传递给另一个组件。数据是一个json对象数组,其中包含数据。我知道数据在那里,因为当我单击按钮时,会调用handleclick方法并显示数据,但当我尝试将数据作为数组传递(季节显示和季节长度)时,我得到一个错误:错误:重新渲染过多。React限制渲染的数量以防止无限循环。如何将数据作为数组从fetch传递到下拉框组件 function HomeScreen({ navigation }) { const [list, setList] = useState([]); con
function HomeScreen({ navigation }) {
const [list, setList] = useState([]);
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
let seasons = [];
useEffect(() => {
fetch('http://localhost:3000/person', {
credentials: "same-origin"
})
.then((response) => response.json())
.then((json) => setData(json))
.catch((error) => console.log("junk" + error))
.finally(() => setLoading(false));
}, []);
const [display, setDisplay] = useState("");
const handleClick = () => {
setDisplay(
data.map((item, i) => (
<Text key={i}> {item.lastname} </Text>
)))
}
const [seasondisplay, setSeasonDisplay] = useState("");
const [seasonlength, setSeasonLengthDisplay] = useState(0);
setSeasonDisplay(
data["lastname"]
)
setSeasonLengthDisplay (
2
)
return (
<View style={styles.container}>
<View style={styles.innerContainer}>
<DropDownChoice whichDropDown="Season" HowMany={seasonlength} ItemChoices={seasondisplay} />
<DropDownChoice whichDropDown="Veggies" HowMany={4} ItemChoices={["carrots", "turnip", "peas", "corn"]} />
<DropDownChoice whichDropDown="Fruit" HowMany={3} ItemChoices={["apples", "oranges", "bananas"]} />
<Button
title="Connect to DB"
onPress={() => {
console.log("please work");
console.log(data);
handleClick();
}}
功能主屏幕({navigation}){
const[list,setList]=useState([]);
const[isLoading,setLoading]=useState(true);
const[data,setData]=useState([]);
让季节=[];
useffect(()=>{
取('http://localhost:3000/person', {
凭据:“相同来源”
})
.then((response)=>response.json())
.然后((json)=>setData(json))
.catch((错误)=>console.log(“垃圾”+错误))
.最后(()=>setLoading(false));
}, []);
const[display,setDisplay]=useState(“”);
常量handleClick=()=>{
设置显示(
data.map((项目,i)=>(
{item.lastname}
)))
}
常数[季节显示,设置季节显示]=使用状态(“”);
常数[季节长度,设置季节长度显示]=使用状态(0);
设置seasondisplay(
数据[“lastname”]
)
设置季节长度显示(
2.
)
返回(
{
控制台日志(“请工作”);
控制台日志(数据);
handleClick();
}}
以下是json:
[{“personid”:11,“lastname”:“cook”,“firstname”:“ben”,“address”:“north”,“city”:“london”},{“personid”:22,“lastname”:“smith”,“firstname”:“elaine”,“address”:“main”,“city”:“milton”}]我怀疑:
您正在通过设置seasondisplay更改每个渲染的状态。此状态更改会导致重新渲染,并在渲染时再次更改状态。您有一个循环
function HomeScreen({ navigation }) {
const [list, setList] = useState([]);
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState([]);
let seasons = [];
useEffect(() => {
fetch('http://localhost:3000/person', {
credentials: "same-origin"
})
.then((response) => response.json())
.then((json) => setData(json))
.catch((error) => console.log("junk" + error))
.finally(() => setLoading(false));
}, []);
const [display, setDisplay] = useState("");
const handleClick = () => {
setDisplay(
data.map((item, i) => (
<Text key={i}> {item.lastname} </Text>
)))
}
const [seasondisplay, setSeasonDisplay] = useState("");
const [seasonlength, setSeasonLengthDisplay] = useState(0);
//---------- changes
useEffect(() => {
setSeasonDisplay(data["lastname"])
setSeasonLengthDisplay (2)
}, [data])
//----------------
return (
<View style={styles.container}>
<View style={styles.innerContainer}>
<DropDownChoice whichDropDown="Season" HowMany={seasonlength} ItemChoices={seasondisplay} />
<DropDownChoice whichDropDown="Veggies" HowMany={4} ItemChoices={["carrots", "turnip", "peas", "corn"]} />
<DropDownChoice whichDropDown="Fruit" HowMany={3} ItemChoices={["apples", "oranges", "bananas"]} />
<Button
title="Connect to DB"
onPress={() => {
console.log("please work");
console.log(data);
handleClick();
}}
功能主屏幕({navigation}){
const[list,setList]=useState([]);
const[isLoading,setLoading]=useState(true);
const[data,setData]=useState([]);
让季节=[];
useffect(()=>{
取('http://localhost:3000/person', {
凭据:“相同来源”
})
.then((response)=>response.json())
.然后((json)=>setData(json))
.catch((错误)=>console.log(“垃圾”+错误))
.最后(()=>setLoading(false));
}, []);
const[display,setDisplay]=useState(“”);
常量handleClick=()=>{
设置显示(
data.map((项目,i)=>(
{item.lastname}
)))
}
常数[季节显示,设置季节显示]=使用状态(“”);
常数[季节长度,设置季节长度显示]=使用状态(0);
//----------变化
useffect(()=>{
setSeasonDisplay(数据[“lastname”])
设置季节长度显示(2)
},[数据])
//----------------
返回(
{
控制台日志(“请工作”);
控制台日志(数据);
handleClick();
}}