我想知道如何使用 Reactrefs
导航到特定组件
function App() {
let CompetencesRef = React.createRef();
let ExperiencesRef = React.createRef();
let FormationRef = React.createRef();
let RecoRef = React.createRef();
let refs = { CompetencesRef, ExperiencesRef, FormationRef, RecoRef }
let scrollToRef = (reference) => {
console.log(reference)
reference.current.scrollIntoView({ behavior: 'smooth' })
}
return (
<ParallaxProvider>
<ThemeProvider theme={theme}>
<div className="App">
<div className="hero">
<HeaderApp refs={refs} scrollToRef={scrollToRef} />
<ApprochApp />
</div>
<Apropos />
<Competences ref={CompetencesRef} />
<Experiences ref={ExperiencesRef} />
<Formation ref={FormationRef} />
<Recom ref={RecoRef} />
<Contact />
<Footer />
</div >
</ThemeProvider>
</ParallaxProvider>
);
}
export default App;
应用标头
const AppHeader = (props, refs, scrollToRef) => {
console.log(props)
return (
<div >
<Headroom>
<MenuApp refs={props.refs} scrollToRef={props.scrollToRef} />
</Headroom>
</div>
)
}
export default AppHeader
应用菜单
const MenuApp = (props, refs, scrollToRef) => {
console.log(props)
return (
<div className="menu sticky-inner grid-container">
<div className="desktop-menu">
<div className="menu-item a-propos" ref={props.refs.CompetencesRef} onClick={() => props.scrollToRef(props.refs.CompetencesRef)}>
<p className='button'>Compétences</p>
</div>
<div className="menu-item competences" ref={props.refs.ExperiencesRef} onClick={() => props.scrollToRef(props.refs.ExperiencesRef)} >
<p className='button'>Experiences </p>
</div>
<div className="menu-item experiences" ref={props.refs.FormationRef} onClick={() => props.scrollToRef(props.refs.FormationRef)}>
<p className='button'>Formation</p>
</div>
<div className="menu-item formation" ref={props.refs.RecoRef} onClick={() => props.scrollToRef(props.refs.RecoRef)}>
<p className='button'>Recomendations </p>
</div>
</div>
<p className="mobile-menu">
<MenuIcon />
</p>
<div className="github-ico">
<GitHubIcon />
</div>
<div className="linkedin-ico">
<LinkedInIcon />
</div>
<div className="contact">
<div className='contact-btn'>
<span className="contact-ico"> <MessageIcon /></span> <span style={isBrowser ? { display: 'block' } : { display: 'none' }} > contact </span>
</div>
</div>
</div>
)
}
export default React.forwardRef(MenuApp)
我在 Apps.js 中使用 ClickHandler 和scrollIntoView
. 但实际上当我单击视图时不会滚动。
我不会使用像react-scroll这样的包