[英]I have a function component and can't figure out how to import into a router, reactjs
I have a Slider component function, export default Slider. In App.js I have我有一个 Slider 组件 function,默认导出 Slider。在 App.js 中我有
function App() {
return (
<Router>
<Routes>
<Route exact path='/' element={<Home />} />
<Route path='/about' element={<About/>} />
<Route path='/contact' element={<Contact/>} />
<Route path='/blogs' element={<Blogs/>} />
<Route path='/sign-up' element={<SignUp/>} />
</Routes>
</Router>
);
}
How would I go about having the component function run when the '/' Homepage loads?我 go 如何让组件 function 在“/”主页加载时运行?
This is the component function. Noob and having trouble finding the correct terms to search a proper documentation.这是组件 function。Noob 并且无法找到正确的术语来搜索正确的文档。
function Slider() {
const messages = [
"Around 920,000 cats and dogs were euthanized in animal shelters in 2020...",
"Approximately 3.1 million dogs and 3.2 million cats enter animal shelters each year...",
"Most pets are surrendered for adoption due to no fault of their own...",
"If you'd like to take a leap towards adopting a pet, click on the green paw!"
];
let i = 0;
const slides = document.querySelector('#slides');
function slide() {
slides.innerHTML = messages[i];
slides.style.opacity = 1;
setTimeout(next, 4000);
}
function next() {
i++;
if (i > messages.length - 1) {
i = 0;
} else if (i === messages[3]) {
setTimeout(10000);
}
slides.style.opacity = 0;
setTimeout(slide, 2000);
}
return (
Slider()
);
}
export default Slider;
You can use this Slider component directly into the Home component because I'm assuming that this #slides
element is on Home.您可以将这个 Slider 组件直接用于 Home 组件,因为我假设这个#slides
元素在 Home 上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.