我想在我的头中使用链接滚动到我的应用程序的不同部分与scrollIntoView。标题是App的一个孩子。我得到一个TypeError,说我试图保存id的变量是未定义的。有人可以帮助我确定我做错了什么吗?我想我可能不得不使用ComponentDidMount,但我不知道如何去做,如果这是修复。我将不得不这样做我的所有标题链接。如何在React中使用scrollIntoView?
//错误 bundle.js:152遗漏的类型错误:在App.getScrollLocations无法读取属性 'scrollIntoView' 空 的(bundle.js:152) 在的onClick(bundle.js:19957) 的对象.ReactErrorUtils.invokeGuardedCallback(bundle.js:4660) 在executeDispatch(bundle.js:4460) 在Object.executeDispatchesInOrder(bundle.js:4483) 在executeDispatchesAndRelease(bundle.js:3913) 在executeDispatchesAndReleaseTopLevel(bundle.js :3924) at Array.forEach() at forEachAccumulated(bundle.js:4760) at Obje ct.processEventQueue(bundle.js:4129) ///////
//应用
class App extends Component {
constructor(props) {
super(props);
this.closeModal = this.closeModal.bind(this);
this.openModal = this.openModal.bind(this);
this.getScrollLocations = this.getScrollLocations.bind(this);
this.state = {
open: false,
projects: Service,
selectedProject: Service[0]
}
}
closeModal(event) {
this.setState({open: false});
}
openModal(project) {
this.setState({
open: true,
selectedProject: project
});
}
///////////// scroll function //////////////
getScrollLocations() {
const whatIDo = document.getElementById('.whatIdo');
console.log(whatIDo)
whatIDo.scrollIntoView();
}
render() {
const show = {
display: 'block'
};
const hide = {
display: 'none'
};
return (
<div>
<div style={this.state.open === false ? hide : show}>
<Modal
value={this.state.open}
closeModal={this.closeModal}
project={this.state.selectedProject}
/>
</div>
<Header
//////////// FUNCTION PASSED TO HEADER ///////////////
getScrollLocations={this.getScrollLocations}
/>
<Intro />
/////////////// ELEMENT I AM TARGETING /////////////////
<WhatIDo id="whatIDo" />
<WhoIAm />
<Gallery
value={this.state.open}
projects={this.state.projects}
openModal={this.openModal}
/>
<Contact />
<Footer />
</div>
);
}
}
//部首
const header = (props) => {
console.log(props);
return (
<div className="header">
<div className="header-name">
XXXXXXX XXXXXXX
</div>
<div className="header-links">
<ul>
<li>Intro</li>
<li
///////////// FUNCTION CALL ON CLICK /////////////////
onClick={() => props.getScrollLocations()}
>What I do</li>
<li>Who I am</li>
<li>My Work</li>
<li>Contact</li>
</ul>
</div>
</div>
)
}
好吧,我会试试这个。谢谢。 – jcs1977
什么是这个文件:import scrollIntoViewIfNeeded从'scroll-into-view-if-needed';? – jcs1977
这是一个模块,用于检测浏览器页面是否在本机支持ScrollIntoView,如果浏览器不支持scrollIntoview,那么该模块不执行任何操作,如果用户浏览器不支持scrollIntoView,则该模块提供一个polyfill来启用特征。 这个模块的更多文档: https://www.npmjs.com/package/scroll-into-view-if-needed 为ScrollIntoView 浏览器支持: http://caniuse.com/#feat=scrollintoview –