【问题标题】:Toggling an :active class with ReactJS使用 ReactJS 切换 :active 类
【发布时间】:2017-01-12 15:02:54
【问题描述】:

我正在实现一个引导屏幕,用户在该屏幕中选择 3 个以上的项目,并且在用户单击它后,一个紫色的叠加层会保留下来。

像这样:

这是我的代码:

var RepeatModule = React.createClass({
   getInitialState: function() {
      return { items: this.props.items || [] }
   },
   componentWillMount: function() {
     console.log("componentWillMount()")
     this.setState({ items : data })
     console.log(data,"data is here");
   },
   handleClick: function (e, item) {
    this.setState({active: true});
   },
   render: function() {
      var listItems = this.state.items.map(function(item) {
         return (
            <ListItem item={item}/>
         );
      });
      return (
         <div className="flex-container">
             {listItems}
         </div>
      );
   }
});
/* make the items stateless */
var ListItem = function(props) {
    var backgroundImage = {
      backgroundImage: 'url(' + props.item.pictureURL + ')'
    };
     return (
         <div className="block-grid-item">
          <a className="display-block card text-gray-darker">
            <div onClick={handleClick} style={backgroundImage} className="card-img-wrap m-xs-0 placeholder placeholder-landscape">
            </div>
            <div className="card-meta">
              <div className="vesta-hp-gifts-card-title text-gray-darker">{props.item.storeName}</div>
            </div>
          </a>
        </div>
     );
}

我尝试在我的 handleClick 函数中保持活动状态时遇到问题。

这是我的悬停 CSS 效果

.card-img-wrap::after
  {
    background-color: #000;
    bottom: 0px;
    content: "";
    left: 0px;
    opacity: 0;
    position: absolute;
    right: 0px;
    top: 0px;
  }
  .card-img-wrap:hover::after
  {
    background-color: #000;
    bottom: 0px;
    content: "";
    left: 0px;
    opacity: 0;
    position: absolute;
    right: 0px;
    top: 0px;
    opacity: 0.6;
    background-color: #5450DC;
  }

为什么这不起作用?如何做到这一点?

【问题讨论】:

  • 您没有将handleClick 传递给ListItem。即使那样,您也没有跟踪活动项目。
  • @xiaofan2406 如何将handleClick 传递给ListItem?

标签: javascript html css reactjs


【解决方案1】:

您必须将属性传递给 ListItem,例如handleClick

   render: function() {
      var listItems = this.state.items.map(function(item) {
         return (
            <ListItem handleClick={this.handleClick} item={item}/>
         );
      });
      return (
         <div className="flex-container">
             {listItems}
         </div>
      );
   }

然后它将在ListItem中的props下可用

var ListItem = function(props) {
    var backgroundImage = {
      backgroundImage: 'url(' + props.item.pictureURL + ')'
    };
     return (
         <div className="block-grid-item">
          <a className="display-block card text-gray-darker">
            <div onClick={props.handleClick} style={backgroundImage} className="card-img-wrap m-xs-0 placeholder placeholder-landscape">
            </div>
            <div className="card-meta">
              <div className="vesta-hp-gifts-card-title text-gray-darker">{props.item.storeName}</div>
            </div>
          </a>
        </div>
     );
}

你应该使用 active 属性,或者在 ListItem 中控制它,或者通过父属性传递它(取决于你的需要),根据这个属性你应该添加/删除类名。

jsFiddle:https://jsfiddle.net/69z2wepo/67316/

请注意,我正在使用 ES6 功能以保留上下文,您可以使用绑定或其他方法,因此 this.handleClick 将引用类函数

【讨论】:

  • 不幸的是,我仍然收到 Uncaught TypeError: Cannot read property 'handleClick' of undefined
  • 试图弄清楚。这是为什么呢?
  • 在ListItem的props下可用。尝试使用传递给 ListItem 的构造函数(而不是 this.props)的 props 道具,或者也使用 React.createClass 创建 ListItem,我已经编辑了我的答案
  • 看起来 handleClick 在 ListItem 中仍未定义
  • 我添加了一个 jsFiddle
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 2017-02-11
  • 1970-01-01
相关资源
最近更新 更多