Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更改已单击的图像[react router v4]_Javascript_Reactjs_Ecmascript 6_React Router - Fatal编程技术网

Javascript 如何更改已单击的图像[react router v4]

Javascript 如何更改已单击的图像[react router v4],javascript,reactjs,ecmascript-6,react-router,Javascript,Reactjs,Ecmascript 6,React Router,我正在使用反应路由器v4进行路由。链接应用于图像中。单击该图像时(如果它处于活动状态),我想更改该图像 下面是显示我想要的概念的代码 class PrivateServiceType extends Component { render() { console.log('context', this.context.router); let image = isActive ? <img src={IconHouseSelected} alt="apart

我正在使用反应路由器v4进行路由。链接应用于图像中。单击该图像时(如果它处于活动状态),我想更改该图像

下面是显示我想要的概念的代码

class PrivateServiceType extends Component {
  render() {
    console.log('context', this.context.router);
    let image = isActive ? 
      <img src={IconHouseSelected} alt="apartamentos" className="img-responsive" /> :
      <img src={IconHouseNotSelected} alt="apartamentos" className="img-responsive" />
    return (
        <div className="row text-center">
          <div className="col-xs-12 col-sm-12 col-md-4 serviceImg">
            <Link to="/apartamentos">
              {image}
              <h4>APARTAMENTOS</h4>
            </Link>
          </div>
      )
}

PrivateServiceType.contextTypes = {
  router: React.PropTypes.object
}
类PrivateServiceType扩展组件{
render(){
log('context',this.context.router);
让image=isActive?
:
返回(
{image}
公寓
)
}
PrivateServiceType.contextTypes={
路由器:React.PropTypes.object
}

currentUrl
存储为状态对象,并将其分配为
img
src
。在img上单击
一次
处理程序以切换
currentUrl

class PrivateServiceType extends Component {

  constructor(){
    super();

    this.state = {
        iconhouseUrl = 'notselected.png'
     }

    this.toggleIcon = this.toggleIcon.bind(this);
   }

  render() {
    console.log('context', this.context.router);
    return (
        <div className="row text-center">
          <div className="col-xs-12 col-sm-12 col-md-4 serviceImg">
            <Link to="/apartamentos">
              <img src={this.state.iconhouseUrl} alt="apartamentos" className="img-responsive" onClick={this.toggleIcon}/>
              <h4>APARTAMENTOS</h4>
            </Link>
          </div>
      )
   }

  toggleIcon(){
     if(this.state.iconhouseUrl === 'notselected.png')
         this.setState({iconhouseUrl:'selected.png'})
     else
         this.setState({iconhouseUrl:'notselected.png'})
  }

}

PrivateServiceType.contextTypes = {
  router: React.PropTypes.object
}

这样做的目的是将当前路径(包含在
location.pathname
)与条件进行比较,并相应地设置图标。

currentUrl
存储为状态对象,并将其指定为
img
src
。在img上单击
处理程序以切换
currentUrl

class PrivateServiceType extends Component {

  constructor(){
    super();

    this.state = {
        iconhouseUrl = 'notselected.png'
     }

    this.toggleIcon = this.toggleIcon.bind(this);
   }

  render() {
    console.log('context', this.context.router);
    return (
        <div className="row text-center">
          <div className="col-xs-12 col-sm-12 col-md-4 serviceImg">
            <Link to="/apartamentos">
              <img src={this.state.iconhouseUrl} alt="apartamentos" className="img-responsive" onClick={this.toggleIcon}/>
              <h4>APARTAMENTOS</h4>
            </Link>
          </div>
      )
   }

  toggleIcon(){
     if(this.state.iconhouseUrl === 'notselected.png')
         this.setState({iconhouseUrl:'selected.png'})
     else
         this.setState({iconhouseUrl:'notselected.png'})
  }

}

PrivateServiceType.contextTypes = {
  router: React.PropTypes.object
}

这样做的目的是将当前路径(包含在
location.pathname
中)与条件进行比较,并相应地设置图标。

感谢您的帮助。不过,我认为这不是一种可靠的方法。因为我使用了路由器来路由不同的表单。共有3个图像/图标(公寓、租车、体验)。单击公寓图像时,将显示公寓的表单,与其他表单类似。您提供的解决方案有效,但当我单击租车时,公寓的图像将显示为选中,但不应显示。此外,在选中图像后,当我再次单击时,它将切换为未选中。这就是为什么我尝试使用路由器上下文来跟踪它是否正确是否处于活动状态。如果处于活动状态,则显示所选图像,否则显示非所选图像。使用路由敏感方式更新答案。我只显示了公寓,但您也可以通过修改它来将其用于其他两条路由。我没有在可以使用的地方使用以前版本的react router,并且可以使用this.props.path。很抱歉,我没有理解那么如何在react router v4中使用this.props呢?使用this.props.path我得到了未定义的。我使用了location.pathname=='/ApartmentOS'?'houseselected.png':'houseunselected.png'我得到了一个错误,无法读取未定义(…)的属性'length'谢谢你的帮助。但我认为这不是一个可靠的方法。因为我使用路由器来路由不同的表单。有3个图像/图标(公寓、租车、体验)。单击公寓图像时,将显示公寓的表单,与其他表单类似。您提供的解决方案有效,但当我单击租车时,公寓的图像将显示为选中,但不应显示。此外,在选中图像后,当我再次单击时,它将切换为未选中。这就是为什么我尝试使用路由器上下文来跟踪它是否正确是否处于活动状态。如果处于活动状态,则显示所选图像,否则显示非所选图像。使用路由敏感方式更新答案。我只显示了公寓,但您也可以通过修改它来将其用于其他两条路由。我没有在可以使用的地方使用以前版本的react router,并且可以使用this.props.path。很抱歉,我没有理解那么如何在react router v4中使用this.props呢?使用this.props.path我得到了未定义的。我使用了location.pathname=='/ApartmentOS'?'houseselected.png':'houseunselected.png'我得到了一个错误,无法读取未定义(…)的属性'length'