Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

React hook 中不需要参与渲染的数据也要放到state中吗


let page = 0
let limit = 10
const Component = () => {
    return (
    
    )
}

就像上图中的page和limit,只用于发送请求,不参与视图渲染,这样放在函数外是否会有问题?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

如果pagelimit对组件来说是个常量,那放在函数外是没问题的。
但是看变量命名,变量page像是用于分页的并且是会发生变量,如果真是这样应该把page放入ref里:

let limit = 10;

const Component = () => {
    const pageRef = React.useRef(0);
    
    return (
    
    )
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...