I am trying to build upon what someone created for me and I am super new to this so be kind!
I have 9 buttons, each one displays a different image when clicked. This part is done and is working perfectly. Now I need each button to also display different text. I Can't quite get it....
So..there's this:
var Form = React.createClass({
getInitialState: function(){
return {shirtState:'button',
image:null,
color:'white',
colornotice: 'white shirt temp',
shirtName:'',
bandName:'',
bandcampUrl:''}
},
handleColorChange: function(e){
e.preventDefault();
color = e.target.value
this.setState({color: color, colornotice: color +' THIS TEXT NEEDS TO CHANGE FOR EACH BUTTON'})
},
The part that says "THIS TEXT NEEDS TO CHANGE FOR EACH BUTTON" is obviously what needs to change depending on which button is clicked.
And here are the buttons:
<div className="buttons">
<button className="color color-white" onClick={this.handleColorChange} value="white"></button>
<button className="color color-black" onClick={this.handleColorChange} value="black"></button>
<button className="color color-blue" onClick={this.handleColorChange} value="blue"></button>
<button className="color color-green" onClick={this.handleColorChange} value="green"></button>
<button className="color color-orange" onClick={this.handleColorChange} value="orange"></button>
<button className="color color-pink" onClick={this.handleColorChange} value="pink"></button>
<button className="color color-purple" onClick={this.handleColorChange} value="purple"></button>
<button className="color color-red" onClick={this.handleColorChange} value="red"></button>
<button className="color color-yellow" onClick={this.handleColorChange} value="yellow"></button>
</div>
So, each button needs to have different, predetermined text that appears in place of this snippet:
{this.state.colornotice}
The image selections that happen on each button clcik that I mentioned are determined the in CSS. That part works perfectly. Here's part of this:
.color-blue{
background: #fff image-url("Blue-Transparent_2300x2415.png");
background-repeat: no-repeat;
background-position: center 0px;
background-size: cover;
and so on... for all 9 buttons.
Hope this makes sense. Thank you for your help!!!
Make use of props
:
(example with ES6 and ES7 syntax, but you will get the idea)
Button.js
import React, { PropTypes } from 'react';
export default class Button {
static propTypes = {
buttonText = PropTypes.string
}
render() {
return (
<button value={this.props.buttonText}></button>
);
}
}
Parent.js
import React, { PropTypes } from 'react';
import Button from './Button';
export default class Parent extends Component {
constructor() {
super();
this.state = {
text: ['red', 'blue', 'orange']
}
}
render() {
let buttons = this.state.text.map(color => <Button buttonText={color} />);
return (
<div>
{buttons}
</div>
);
}
}
Think I got it!
switch(color) {
case 'white':
// do this and that
this.setState({color: color, colornotice: color +' shirt temp1'})
break;
case 'red':
// do this and that
this.setState({color: color, colornotice: color +' shirt temp2'})
break;
default:
this.setState({color: color, colornotice: color +' shirt temp'})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.