0
我想通過從狀態道具通過定位修復,但我不能setState
裏面handleScroll
功能。我不知道如何訪問函數內部的狀態。滾動addeventlistener裏面的訪問類道具
class App extends Component {
state = { fixed: false }
componentDidMount() {
window.addEventListener('scroll', this.handleScroll);
}
componentWillUnmount() {
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll(e) {
let scrollTop = e.srcElement.body.scrollTop;
(scrollTop > 0) ? this.setState({ fixed: true }) : this.setState({ fixed: false });
}
render() {
// grab steps
const { steps } = this.props;
const { fixed } = this.state;
const { scrollToStep } = this;
return ce('div', { className:'allTheSteps' },
ce(pagination, { steps, fixed }),
Object.values(steps).map((step, i) =>
ce(Steps, { step, key: v4(), i }),
)
);
};
};