ReactJSのカスタムイメージトグルボタン
-
02-01-2020 - |
質問
オン状態とオフ状態のための2つの異なる画像の間に切り替わるカスタムイメージボタンを表示するためのこのReactJSコードを持っています。これを行うよりも簡単な方法はありますか?私はCSSがコードの行の線である可能性があることを望んでいましたが、簡単な例を見つけることができませんでした。
以下のコードは、<MyIconButton>
から<MyPartyCatButton>
への状態を渡し、次に<MyHomeView>
に渡します。私のアプリはホームスクリーン上のこれらのカスタムボタンの4つを持つでしょう、そしてそれが私が<MyIconButton>
を感化させる理由です。
BTW - これはモバイルアプリのためのものであり、私は(私自身)を読んだ(そして私自身に気づいた)モバイルブラウザのチェックボックスを使用して本当に遅いです。それがチェックボックスを使用せずにこれを試すことを選択した理由です。
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 !
);
}
});
. 解決
コポネントの「押された」プロップを動的に(あなたのような)を更新した場合、単に
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 'イベントを処理するように関数を渡すことができます。イベント関数引数はイベントオブジェクトですが、ラッパー状態(古いもの(古いもの)でボタン状態がallyReadyされています。コードはそのようなものになります。
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}
/>
}
})
. 所属していません StackOverflow