简体   繁体   English

this.prop.state.location.url 未定义? React Js 链接道具

[英]this.prop.state.location.url is undefined? React Js Link Props

Component.js:组件.js:

<Link to={{ pathname: 'audio-Player', state: { url: 'Demo' } }}> Play</Link>

If I click this Link then the AnitherComponent.js opens and works but if I open this AnitherComponent.js direct, then it says can not read property url at this.props.loaction.url如果我单击此Link ,则AnitherComponent.js将打开并正常工作,但如果我直接打开此AnitherComponent.js ,则会显示无法在 this.props.loaction.url 读取属性 url

AnitherComponent.js:另一个组件.js:

componentDidMount() {
  this.state.song.src = this.props.loaction.url
  console.log(this.state.song)
  this.setState({
    play_img: document.querySelector('#play_img'),
    range: document.querySelector('#range1'),
  })
  const { song, range, play_img, total_time, currentTime } = this.state
}

You are trying to access loaction.url which doesn't exist.您正在尝试访问不存在的loaction.url You can set a default value if it doesn't exist:如果不存在,您可以设置默认值:

this.state.song.src = this.props.loaction?.url ?? "default value";

or或者

this.state.song.src = this.props.loaction && this.props.loaction.url || "default value";

PS you might need plugin for the optional chaining. PS您可能需要用于可选链接的插件。

I tried this:我试过这个:

componentDidMount() {
  this.state.song.src = this.props.location.state.url
    ? this.props.location.state.url
    : ' sdxs'
  console.log(this.state.song)
  this.setState({
    play_img: document.querySelector('#play_img'),
    range: document.querySelector('#range1'),
  })
  const { song, range, play_img, total_time, currentTime } = this.state
}

But it is still not working.但它仍然无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM