Pulsante di commutazione immagine personalizzata in reactjs
-
02-01-2020 - |
Domanda
Ho questo codice reactJS per mostrare un pulsante immagine personalizzato che attiva tra 2 immagini diverse per lo stato ON e OFF.C'è un modo più semplice per farlo?Speravo che CSS potesse essere meno linee di codice, ma non è stato in grado di trovare un semplice esempio.
Il codice seguente passa dallo stato in alto da <MyIconButton>
a <MyPartyCatButton>
quindi su <MyHomeView>
.La mia app avrà 4 di questi pulsanti personalizzati sulla schermata iniziale, motivo per cui ho fatto conto generatoDicetagcode.
BTW - Questo è per un'app mobile e ho letto (e ho notato questo anch'io) è davvero lento usando le caselle di controllo sui browser mobili;Ecco perché ho scelto di provarlo senza utilizzare le caselle di controllo.
ReactJS Codice
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 !
);
}
});
. Soluzione
Se si aggiorna il coponent 'pressato' puntelli dinamicamente (come hai fatto), semplicemente
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
/>
}
})
.
(Modifica: In questo modo, sul componente MyPartyCatButton, è possibile passare la funzione per gestire l'evento 'tuuggleelection'. Argomento della funzione evento è un Oggetto evento , ma hai lo stato del pulsante già presente nello stato del wrapper (quello vecchio, quindi dovresti invertirlo). Il tuo codice sarà qualcosa del genere:
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 )
}
.
)
Ma se non lo fai, usa il Prop For Defult Value:
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}
/>
}
})
.