如何向refusing ReactJS Hooks(版本 16.8.+)添加事件侦听器?
这是一个示例,我尝试使用溢出自动获取某些 div 的滚动位置。updateScrollPosition永远不会被调用。
function Example(props) {
scroller = useRef();
useEffect(() => {
function updateScrollPosition() {
// update the scroll position
}
if (scroller && scroller.current) {
scroller.current.addEventListener("scroll", updateScrollPosition, false);
return function cleanup() {
scroller.current.removeEventListener("scroll", updateScrollPosition, false);
};
}
});
return (
<div ref={scroller}>
<div className="overflow-auto">
some text
</div>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
我感谢您的帮助
如何获取 ReactJS 组件的滚动位置(可滚动)?我想我必须向我的组件添加一个滚动事件监听器,但我被困在那里,因为我不知道如何将它添加到组件 - 只添加到窗口。我还可以通过哪个道具访问滚动位置?
我正在编写一个包含2D矢量对象的程序:
class Obj{
public:
int x;
string y;
};
vector<Obj> a(100);
vector< vector<Obj> > b(10);
Run Code Online (Sandbox Code Playgroud)
我在向量b中存储了一些向量a的值.
当我尝试将其打印出来时出现错误:
for(int i=0; i<b.size(); i++){
for(int j=0; j<b[i].size(); j++)
cout << b[i][j];
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
D:\ main.cpp:91:错误:'operator <<'不匹配(操作数类型为'std :: ostream {aka std :: basic_ostream}'和'__gnu_cxx :: __ alloc_traits> :: value_type {aka Obj} ')cout << b [i] [j]; ^
例如,我将如何在 Material-UI 的 TextField 选择组件上显示01何时MenuItem折叠TextField以及01 - this is first element何时展开?