jeudi 18 juin 2020

check pathname in if condition for setState React

i've implemented a react joyride tour into a finished tool. now iam stuck on the if condition for the steps. The steps are exported in arrays so i can put them on the right site easily.

currently its like this

let { steps } = this.state
const pathname = this.props.location.pathname

if (pathname === '/') { 
  steps = [] 
} else if (pathname === '/matches/' || pathname === '/matches') {
  if (this.props.firstPartClicked === true) {
    steps = matchSiteStepsOne
  } else if (this.props.secondPartClicked === true) {
    steps = matchSiteStepsTwo
  } else {
    steps = matchSiteSteps
  }
} else if (pathname.endsWith('/edit/') && pathname.startsWith('/matches/')) {
  if (this.props.firstPartClicked === true) {
    steps = matchEditorStepsOne
  } else if (this.props.secondPartClicked === true) {
    steps = matchEditorStepsTwo
  } else {
    steps = matchEditorSteps
  }
} else if (pathname.startsWith('/matches/') && pathname.includes('sequence')) {
  if (this.props.firstPartClicked === true) {
    steps = matchSequenceStepsOne
  } else if (this.props.secondPartClicked === true) {
    steps = matchSequenceStepsTwo
  } else {
    steps = matchSequenceSteps
  }
} else if (pathname.startsWith('/matches/') && pathname.match('\\d+')) {
  steps = matchSteps
}

now the problem is that he skip the first step on the 'matchEditorSteps'. except i delete the last else if condition for 'matchSteps'.

i've tried some other conditions but on many things he set the wrong steps. instead of the 'matchEditorSteps' he sets the 'matchSteps'.

i think its beacuse on both i check for else if (pathname.startsWith('/matches/')

and i dont know how to check it the right way. iam happy about every suggestion and help.

Aucun commentaire:

Enregistrer un commentaire