在React Router v4中使用URL参数时,如何在类似路径中的另一个组件已经渲染时,如何避免渲染组件?

时间:2017-11-12 03:11:26

标签: reactjs react-router

我正在尝试为我的应用程序创建RESTful路由,但是当涉及到URL params的路由时我遇到了问题。

以下是我想要的路线:

/accounts
/accounts/new
/accounts/edit
/accounts/:id

这就是我的路由器的样子:

<Router>
  <Route exact path="/accounts" component={AccountsList} />
  <Route exact path="/accounts/new" component={AccountCreate} />
  <Route exact path="/accounts/edit" component={AccountUpdate} />
  <Route exact path="/accounts/:id" component={AccountDetail} />
</Router>

/accounts工作正常,但当我转到/accounts/new/accounts/edit时,它还会呈现AccountDetail,该/accounts/:id应位于/accounts/:id。< / p>

对于记录,我得知这是正确的行为,因为/accounts/new技术上匹配/accounts/edit:id

我的问题是,是否有办法让$('body').prepend('<script src="https://cdnjs.cloudflare.com/ajax/libs/async/2.6.0/async.min.js"></script>'); const scrapeTimer = setInterval(() => { if(typeof async === 'object') { console.log('Async.min.js loaded... Starting scraping'); scrape(); clearInterval(scrapeTimer); } }, 250); const scrape = function() { let x = 0; let lastIndex = 0; let uploadCount = 0; let start = new Date(); async.whilst(() => { if(x === 100) { x = 0; console.log('Reached 100 listings, resetting x'); } return $('.colnum')[x] !== undefined; }, next => { let index = Number($('.colnum')[x].innerText); let element; let offset = false; if(index !== lastIndex + 1) { console.log(`Offset index! Expected index: ${ lastIndex + 1 } got: ${ index } on x: ${ x }`); element = $('.colnum').filter(function(pos) { if($(this).text() !== (lastIndex + 1).toString()) return false; x = pos; index = lastIndex + 1; offset = true; console.log(`Fixed offset index: ${ index } x: ${ x }`); return true; }).parents().filter('.listing'); if(element.length === 0) { console.log('Failed to fix offset'); debugger; } } else { element = $($('.colnum')[x]).parents().filter('.listing'); if(element.length === 0) { console.log('Index corect but no listing element'); debugger; } } const mlsId = element.find('.columnlink').first().text(); let listing = { mlsId, status: (element.find('.status_A').length !== 0) ? 'active':'inactive', price: Number(element.find('.price').first().text().replace(/[^0-9\.-]+/g, '')), address: element.find('.street-address').first().text(), locality: element.find('[ls="csz"]').first().text(), dwellingType: $(element.find('td')[8]).text(), bedrooms: (/\S/.test($(element.find('td')[9]).text())) ? Number($(element.find('td')[9]).text()):null, bathrooms: (/\S/.test($(element.find('td')[10]).text())) ? Number($(element.find('td')[10]).text()):null, area: (/\S/.test($(element.find('td')[11]).text())) ? Number($(element.find('td')[11]).text().replace(/[^0-9\.-]+/g, '')):null, general: '', photos: [] }; $('#iframe_detail').one('load', () => { $('#tab_tour').click(); const photoTimer = setInterval(() => { const images = $('#iframe_tour').contents().find('img'); for(let y = 0; y < images.length; y++) { if(!images[y].id) listing.photos.push(images[y].src); if(images[y].src === 'http://www.flexmls.com/images/nophotoavailable-huge.png') { x++; lastIndex = index; return next(); } } if(listing.photos.length !== 0) { $('#iframe_detail').one('load', () => { const generals = $('#iframe_detail').contents().find('td[style="border: 1px solid rgb(0, 0, 0); text-align: left; letter-spacing: 0pt; vertical-align: top; word-spacing: 0pt;"]'); for(let y = 0; y < generals.length; y++) listing.general += generals[y].innerHTML.replace(/<\/?span[^>]*>/g, '').split('<div></div>').join('\u000A') + '\u000A'; const publics = $('#iframe_detail').contents().find('td[style="border: 1px solid rgb(0, 0, 0);"]'); for(let y = 0; y < publics.length; y++) if(publics[y].innerText.includes('Public Remarks:')) listing.publicRemarks = publics[y].innerText.replace('Public Remarks:', ''); $($('#iframe_detail').contents().find('table[style="border: 1px solid #969696; width: 100%;"]')[0]).find('span[style="font-size: 8pt;"]').splice(4,8).map((detail, index) => { switch(index) { case 0: listing.features = detail.innerText; break; case 1: listing.roomDetails = detail.innerText; break; case 2: listing.facts = detail.innerText; case 3: listing.facts += '\u000A' + detail.innerText; } }); if(index % 25 === 0) { $('#gridC').scrollTop($('#gridC').prop('scrollHeight')); console.log('Scrolling to load more listings...'); } let seconds = (new Date() - start) / 1000; x++; lastIndex = index; uploadCount++; console.log('Uploaded listing: ' + listing.mlsId + ' LPS: ' + (seconds / uploadCount) + ' Total: ' + uploadCount); console.log({ type: 'listing', listing }); next(); }); $('#tab_detail').click(); clearInterval(photoTimer); } }, 250); }); element.find('.columnlink')[0].click(); }, err => { if(err) throw err; console.log({ type: 'done' }); }); };仅与特定模式匹配(仅限数字)。

1 个答案:

答案 0 :(得分:1)

尝试使用reactRouter的< Switch>组件。

  

渲染第一个孩子&lt;路线&GT;或者&lt;重定向&GT;匹配的   位置。

我认为这正是您所寻找的。