Reactjs 存储在reducer中的ref对象没有offsetTop属性
我有一个动态调查表格生成器。我将所有问题的ref对象保存到reducer中的一个数组中。我想访问所有其他页面的参考,以将用户滚动到特定问题。我的问题是,存储的对象没有用于滚动的偏移设置 使用ref的回调和不使用ref的回调没有区别Reactjs 存储在reducer中的ref对象没有offsetTop属性,reactjs,typescript,redux,Reactjs,Typescript,Redux,我有一个动态调查表格生成器。我将所有问题的ref对象保存到reducer中的一个数组中。我想访问所有其他页面的参考,以将用户滚动到特定问题。我的问题是,存储的对象没有用于滚动的偏移设置 使用ref的回调和不使用ref的回调没有区别 class Question extends React.Component<QuestionProps,{}>{ myRef:any; constructor(props:QuestionProps){ super(pro
class Question extends React.Component<QuestionProps,{}>{
myRef:any;
constructor(props:QuestionProps){
super(props);
...
this.myRef = React.createRef(); // Create a ref object
}
...
render(){
const {question,activeQuestionId} = this.props;
const active = activeQuestionId === question.id;
return <Row ref={this.myRef} className="QuestionBox" style={{opacity:active?1:0.2}} onClick={this.handleQuestionClick}>
currentQuestionRef.current是我们可以在控制台中看到的正确对象。
但是currentQuestionRef.current.offsetTop未定义 您应该将
ref
与react组件一起使用,如
、
和。。。如以下示例所示:
render(){
return(
<div ref={this.myRef}>
//code comes here
<div/>
);
}
render(){
返回(
//这里有密码
);
}
或者您可以将ref
作为prop
发送到另一个组件中
render(){
return(
<div ref={this.myRef}>
//code comes here
<div/>
);
}