【问题标题】:How to change the image that has been clicked [react-router v4]如何更改已点击的图片【react-router v4】
【发布时间】:2017-04-03 06:42:48
【问题描述】:

我正在使用 react router 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
}

【问题讨论】:

    标签: javascript reactjs ecmascript-6 react-router


    【解决方案1】:

    currentUrl 存储为状态对象并将其分配为imgsrc。在 img 上放置一个 onClick 处理程序以切换 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
    }
    

    iconhouseUrl 状态变量保存 url,toggleIcon 函数处理绑定到 imgonClick 处理程序的更改

    根据你的评论这里是路由敏感方式

    <Link to="/apartamentos">
              <img src={(location.pathname === '/apartamentos' )?'houseselected.png':'houseunselected.png'} 
               alt="apartamentos" className="img-responsive"/>
              <h4>APARTAMENTOS</h4>
    </Link>
    

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

    【讨论】:

    • 感谢您的帮助。不过,我认为这不是一种可靠的方法。因为我使用路由器来路由不同的形式。有 3 个图像/图标(公寓、汽车租赁、体验)。单击公寓图像时,将显示公寓表格并且与其他表格类似。您提供的解决方案有效,但是当我点击汽车租赁时,公寓的图片显示为选中但不应该。
    • 同样在被选中图像后,当我再次点击它时,它会切换到未选中状态。这就是为什么我尝试使用路由器上下文来跟踪它是否处于活动状态。如果它处于活动状态,则显示选定的图像,否则显示非选定的图像。
    • 用路由敏感的方式更新了答案。我只展示了公寓,但您也可以通过修改它来将其用于其他两条路线。
    • 我没有使用过以前版本的 react 路由器,我可以在其中执行 并且可以使用 this.props.path。抱歉,我不明白如何在反应路由器 v4 中使用 this.props?
    • 使用 this.props.path 我得到未定义。我使用 location.pathname === '/apartamentos' ? 'houseselected.png':'houseunselected.png' 我得到了一个错误,无法读取 undefined(...) 的属性 'length'
    猜你喜欢
    • 2017-08-20
    • 2019-08-21
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多