javascript - ReactJS 中的自定义图像切换按钮

标签 javascript css reactjs

我有这个 ReactJS 代码来显示一个自定义图像按钮,该按钮在 2 个不同图像之间切换,用于打开和关闭状态。有没有更简单的方法来做到这一点?我希望 CSS 可以减少代码行数,但无法找到一个简单的示例。

下面的代码从<MyIconButton>向上传递状态至 <MyPartyCatButton>然后到<MyHomeView> .我的应用程序将在主屏幕上有 4 个这样的自定义按钮,这就是我排除 <MyIconButton> 的原因.

顺便说一句——这是一个移动应用程序,我读到(并且自己注意到了这一点)在移动浏览器上使用复选框真的很慢;这就是为什么我选择在不使用复选框的情况下进行尝试的原因。

ReactJS 代码

var MyIconButton = React.createClass({

  handleSubmit: function(e) {
    e.preventDefault();
    console.log("INSIDE: MyIconButton handleSubmit");

    // Change button's state ON/OFF, 
    // then sends state up the food chain via            
    //  this.props.updateFilter( b_buttonOn ).
    var b_buttonOn = false;
    if (this.props.pressed === true) {
      b_buttonOn = false;
    }
    else {
      b_buttonOn = true;
    }
    // updateFilter is a 'pointer' to a method in the calling React component.
    this.props.updateFilter( b_buttonOn ); 
  },

  render: function() {

    // Show On or Off image.
    // ** I could use ? : inside the JSX/HTML but prefer long form to make it explicitly obvious. 
    var buttonImg = "";
    if (this.props.pressed === true) {
      buttonImg = this.props.onpic;
    }
    else {
      buttonImg = this.props.offpic;
    }

    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <input type="image" src={buttonImg}></input>
        </form>
      </div>
    );
  }
});


// <MyPartyCatButton> Doesn't have it's own state, 
// passes state of <MyIconButton> 
// straight through to <MyHomeView>.
var MyPartyCatButton = React.createClass({

  render: function() {
    return (
      <MyIconButton pressed={this.props.pressed} updateFilter={this.props.updateFilter} onpic="static/images/icon1.jpeg" offpic="static/images/off-icon.jpg"/>
    );
  }
});

//
// Main App view
var MyHomeView = React.createClass({
  getInitialState: function() {
    // This is where I'll eventually get data from the server.
    return {
      b_MyPartyCat: true
    };
  },

  updatePartyCategory: function(value) {
    // Eventually will write value to the server.
    this.setState( {b_MyPartyCat: value} );
    console.log("INSIDE: MyHomeView() updatePartyCategory() " + this.state.b_MyPartyCat );
  },

  render: function() {
    return (
        <div>
         <MyPartyCatButton pressed={this.state.b_MyPartyCat} updateFilter={this.updatePartyCategory}/>
        </div>

        // Eventually will have 3 other categories i.e. Books, Skateboards, Trees !
    );
  }
});

最佳答案

如果您动态更新组件“按下” Prop (就像您所做的那样),只需

var MyIconButton= React.createClass({
    render: function(){
        var pic= this.props.pressed? this.props.onpic : this.props.offpic
        return <img 
            src={pic} 
            onClick={this.props.tuggleSelection}  //updateFilter is wierd name
        />
    }
})

(编辑:这样,在 MyPartyCatButton 组件上,您可以传递函数来处理 'tuggleSelection' 事件。事件函数参数是一个 event object ,但是您已经在包装器状态中准备好按钮状态(旧的,所以你应该反转它)。你的代码将是这样的:

render: function(){
    return <MyIconButton pressed={this.state.PartyCatPressed} tuggleSelection={this.updatePartyCategory} />
}
updatePartyCategory: function(e){
    this.setState( 
        {PartyCatPressed: !this.state.PartyCatPressed} //this invert PartyCatPressed value
    );
    console.log("INSIDE: MyHomeView() updatePartyCategory() " + this.state.b_MyPartyCat )
}

)

但如果你不这样做,请使用 prop 作为默认值:

var MyIconButton= React.createClass({
    getInitialState: function(){
        return {pressed: this.props.defultPressed}
    },
    handleClick: function(){
        this.setState({pressed: !this.state.pressed})
    },

    render: function(){
        var pic= this.state.pressed? this.props.onpic : this.props.offpic
        return <img 
            src={pic} 
            onClick={this.handleClick}
        />
    }
})

关于javascript - ReactJS 中的自定义图像切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25089661/

相关文章:

带有多个等待的 JavaScript 异步

javascript - 使用 sketch.js 为移动网页绘制 Canvas 会在触摸时重置

javascript - jQuery - 获取屏幕尺寸宽度

javascript - Sharepoint 管理中心 Ajax 发布到 WebMethod 返回 此页面的安全验证无效

html - 如果可能仅按图像 HTML/CSS 对齐箭头

html - 嵌入式 Google Drive 内容扩展到 iOS 设备上自己的容器之外

html - 具有 `srcset` 属性的自动宽度图像

javascript - 重新启动组件上的 Audio() 流已安装

reactjs - 如何在react js中添加动态CSS

reactjs - shouldComponentUpdate 会阻止 getDerivedStateFromProps 吗?