Javascript 使用状态更新数组
我想从谷歌电子表格中获取数据,但我对状态有一个问题 我有一个位置列表和Url列表。位置1:url 1和位置2:url 2的示例 第一次渲染: 默认情况下,我获取位置1和从url 1获取的数据Javascript 使用状态更新数组,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,我想从谷歌电子表格中获取数据,但我对状态有一个问题 我有一个位置列表和Url列表。位置1:url 1和位置2:url 2的示例 第一次渲染: 默认情况下,我获取位置1和从url 1获取的数据 console.log(getTimes)&console.log(times)从url 1返回位置1的数据 单击位置5: 我从url 1获取数据,但我希望从url 5获取数据 console.log(getTimes)从Url 5返回数据 console.log(次)从Url 1返回数据 单击位置7: 我
console.log(getTimes)
&console.log(times)
从url 1返回位置1的数据
单击位置5:
我从url 1获取数据,但我希望从url 5获取数据
console.log(getTimes)
从Url 5返回数据
console.log(次)
从Url 1返回数据
单击位置7:
我从Url 5(预览状态!)获取数据
console.log(getTimes)
从Url 7返回数据
console.log(次)
从Url 5返回数据
单击位置44:
当我更改位置44时,我现在从url 7获取数据
console.log(getTimes)
从Url 44返回数据
console.log(次)
从Url 7返回数据
==Daily.js==
function Daily({ locationProps = 1, root }) {
const context = useContext(ThemeContext);
const localization = useCallback(() => {
if (root && cookies.get("location") !== undefined) {
return cookies.get("location");
}
return locationProps;
}, [locationProps, root]);
const _data = useRef(new Data());
useEffect(() => {
_data.current.getTimesFromGoogleSheets();
}, [locationProps]);
const getTimes =()=> _data.current.getTimes();
const times = useState(()=>getTimes()); <------- here is the problem useState dont update
**==data.js==**
根据我目前所知,你更可能在寻找这样的东西:
function Daily({ locationProps = 1, root }) {
const context = useContext(ThemeContext);
const localization = useCallback(() => {
if (root && cookies.get("location") !== undefined) {
return cookies.get("location");
}
return locationProps;
}, [locationProps, root]);
const _data = useRef(new Data());
// Use times to display, use setTimes to change the data, maybe as part of your effect?
const [times, setTimes] = useState(_data.current.getTimes());
useEffect(() => {
_data.current.getTimesFromGoogleSheets();
const newTimes = _data.current.getTimes();
setTimes(newTimes);
}, [locationProps]);
// times.map(...)
}
我认为您使用的useState钩子是错误的,接口是这样的
const[state,updateStateFunction]=useState(initialValue)
。如果initialValue是一个函数,则在第一次渲染之前会调用该函数一次,然后再也不会调用该函数。因此,您可能希望在每个渲染上使用updateStateFunction来更改该状态。是的,但当我这样编写时,会出现错误“settimes.map不是函数”,因为settimes不是数组,它是函数,也许如果您共享更多代码,我可以进一步帮助您。{settimes.map((time,index)=>({time[Name]}}const[times,settimes]=useState(getTimes());useffect(()=>{settimes(getTimes())}[getTimes()])
很乐意帮助!如果没有其他问题,您可以接受答案peerror:times.map不是函数:/好的,然后我会检查\u data.current.getTimes()
返回,因为时间将等于该值。如果它不返回数组,则映射将不起作用,而是从检查开始!-data.curent.getTimes()返回{Date:“03/05/2020”,data1:“0”,data2:“0”…}Date:“03/05/2020”data1:“0”data2:“0”
因此,如果不传递日期参数,您将获得完整的数据集,该数据集是在getData函数JSON.parse(_data)
中从JSON.parse创建的对象。您不能在对象上使用.map
,但可以使用类似Object.entries(times).map(…)
function Daily({ locationProps = 1, root }) {
const context = useContext(ThemeContext);
const localization = useCallback(() => {
if (root && cookies.get("location") !== undefined) {
return cookies.get("location");
}
return locationProps;
}, [locationProps, root]);
const _data = useRef(new Data());
// Use times to display, use setTimes to change the data, maybe as part of your effect?
const [times, setTimes] = useState(_data.current.getTimes());
useEffect(() => {
_data.current.getTimesFromGoogleSheets();
const newTimes = _data.current.getTimes();
setTimes(newTimes);
}, [locationProps]);
// times.map(...)
}