[英]react useRef not getting assigned
So i'm trying to create a ref using the indexes of 'todos' prop but for some reason useRef doesn't work with it, what am i doing wrong?所以我正在尝试使用'todos'道具的索引创建一个参考,但由于某种原因 useRef 不能使用它,我做错了什么?
here's the example这是示例
import React, { useState, useRef } from 'react'
function Todos({todos}) {
/* todos prop is an array of objects */
/* FYI todos.map((_, index) => index) returns [0, 1, 2]*/
/* CASE 1 */
const order = useRef(todos.map((_, index) => index))
console.log(order) /* returns {current: []} */
/* CASE 2 */
const order = useRef([0, 1, 2])
console.log(order) /* returns {current: [0,1,2]} */
}
I found a working solution.我找到了一个可行的解决方案。 for some reason setting the current property of the order seems to work
出于某种原因,设置订单的当前属性似乎有效
const order = useRef();
order.current = todos.map((_, index) => index)) /* works!!!*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.