【发布时间】:2017-10-11 00:00:51
【问题描述】:
我正在尝试使用标题中的链接通过 scrollIntoView 滚动到我的应用程序的不同部分。标头是 App 的子项。我收到一个 TypeError 说我试图将 id 保存到的变量是未定义的。有人可以帮我确定我做错了什么吗?我想我可能不得不使用 ComponentDidMount,但我不知道该怎么做,如果这甚至是修复。我将不得不对我所有的标题链接执行此操作。
//错误 bundle.js:152 未捕获的类型错误:无法读取属性“scrollIntoView” 空值 在 App.getScrollLocations (bundle.js:152) 在 onClick (bundle.js:19957) 在 Object.ReactErrorUtils.invokeGuardedCallback (bundle.js:4660) 在 executeDispatch (bundle.js:4460) 在 Object.executeDispatchesInOrder (bundle.js:4483) 在 executeDispatchesAndRelease (bundle.js:3913) 在 executeDispatchesAndReleaseTopLevel (bundle.js:3924) 在 Array.forEach () 在 forEachAccumulated (bundle.js:4760) 在 Object.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>
)
}
【问题讨论】:
标签: javascript reactjs