简体   繁体   English

我有一个 function 组件,不知道如何导入路由器,reactjs

[英]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.

相关问题 我不知道如何正确地将REST响应对象传递给ReactJS子组件 - I can not figure out how to pass a REST response object to a ReactJS child component correctly ReactJS - 无法导入组件 - ReactJS - Can't import component 似乎无法弄清楚如何 get.toggle function 在 jQuery 中工作,包括该库,我有一个用于测试的 jsFiddle - Can't seem to figure out how to get .toggle function working in jQuery, the library is included, I have a jsFiddle for testing 我正在尝试导出一个函数以导入到另一个 javascript 页面,但无法弄清楚如何使用导出/导入来做到这一点 - i am trying to export a function to import into another javascript page but can not figure out how to do it with exports/imports 无法弄清楚如何使用JavaScript在浏览器中导入模块 - Can't figure out how to import modules in browser with javascript nuxt js 样式范围我不知道如何更改另一个组件的样式 - nuxt js style scoped i can't figure out how to change the style of another component 我不知道如何在子组件中使用反应导航 - I can't figure out how to use react-navigation in a child component 我不知道如何制作模态“设置按钮”组件 - I can't figure out how to make a modal 'settings button' component 我不知道如何在另一个函数中调用先前创建的函数? - I can't figure out How to call previously created function inside another function? 如何将MS Word文档内容导入ReactJS组件? - How can i import ms word document content to ReactJS component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM