gpt4 book ai didi

javascript - 将参数传递给 React Router 中的 React 元素

转载 作者:行者123 更新时间:2023-11-30 19:21:51 25 4
gpt4 key购买 nike

我创建了一个名为 Jobscard 的元素,它将 jobsList 作为参数。我还尝试使用 react-router 将此 jobsCard 设置为路由器。

我调查了这个问题,他们似乎在网络上没有类似的问题,所有解决方案和指南都使用 react 组件,但这不适用于 react 元素。这可能吗?

import React from 'react';
import { projectList } from './projectList';
import JobsCard from './JobsCard';
import { jobsList } from './jobsList';
import CardList from './CardList';
import Ham from './hamburger';
import { Route, Switch } from 'react-router-dom';
import { BrowserRouter } from 'react-router-dom';
import jobsList from './jobsList'

const App = () => {
return(

<div className='blue1 center'>

<BrowserRouter>
<Ham />

<Switch>
<Route exact path="/" render{props => <JobsCard jobsList={jobsList}> </JobsCard> }/>

<Route path="/projects" component={<CardList projectList={projectList}/>}/>
</Switch>
</BrowserRouter>


</div>

)
}

export default App;

JobsCard 元素是

import React from 'react';
import Job from './Job.js'

const JobsCard = ({jobsList}) => {
const cardDraw = jobsList.map((proj, i) => {
return <Job key={i} title={jobsList[i].title}
website={jobsList[i].website} description={jobsList[i].description}
logo={jobsList[i].logo} company={jobsList[i].company} years={jobsList[i].years}/>
})
return (
<div className=" cf justify-center ma3">
{cardDraw}


</div>
);
}

export default JobsCard;

jobsList 看起来像这样

import energyxchain from ''
export const jobsList = [
{ title: '',
website: '',
description: '',
logo: ,
company: '',
years: '2018-2019'
},
{
title: '',
company: '',
website: '',
logo: '',
description: '',
years: '2017-2018'
}];

我希望 jobsCard 成为一条路线。

最佳答案

来自 route rendering props :

render, which takes an inline function, should only be used when you have to pass in-scope variables to the component you want to render.

You should not use the component prop with an inline function to pass in-scope variables because you will get undesired component unmounts/remounts.

所以你的路线应该是 ff:

使用 component 属性(不应传递变量):

<Route
exact path="/"
component={JobsCard}
/>

使用 render 属性:

<Route
exact path="/"
render={routeProps => (<JobsCard {...routeProps} jobsList={jobsList} />)}
/>

通常,您应该将导航与内部数据管理分开(使用 redux connect)。

关于javascript - 将参数传递给 React Router 中的 React 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57367724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com