Я новичок в реакции, и мой первый мини-проект - простой игровой автомат. В настоящее время я завершил logi c для отображения случайных смайликов после нажатия кнопки. Следующим шагом для меня перед стилизацией и добавлением logi c для выигрыша / проигрыша и счетчика монет et c является добавление анимации.
https://codepen.io/fmressel/pen/vRLerN
Этот код - это именно то, что мне нужно, но, как вы можете видеть, он структурирован совсем не так, как мой код (ниже), и я выдергиваю волосы, пытаясь понять, как я могу что-то получить похоже на работу для моего проекта.
Любая помощь очень ценится!
import React, { Component } from 'react'
import Contents from './Contents'
import './mainslots.css'
class Slots extends Component {
static defaultProps = {
fruits: ["?", "?", "?", "?", "?", "?"]
};
constructor(props) {
super(props);
this.state = {fruit1: '?', fruit2: '?', fruit3: '?', rolling: false};
this.roll = this.roll.bind(this);
};
roll = () => {
const dFruit1 = this.props.fruits[
Math.floor(Math.random() * this.props.fruits.length)
];
const dFruit2 = this.props.fruits[
Math.floor(Math.random() * this.props.fruits.length)
];
const dFruit3 = this.props.fruits[
Math.floor(Math.random() * this.props.fruits.length)
];
this.setState({fruit1: dFruit1, fruit2: dFruit2, fruit3: dFruit3, rolling: true});
setTimeout(() => {
this.setState({ rolling: false });
}, 700)
}
render(){
return(
<div className="SlotMachine">
<div className="SlotsContainer">
{this.state.fruit1}
{this.state.fruit2}
{this.state.fruit3}
</div>
<button className="spinner" onClick={this.roll} disabled={this.state.rolling}>
{this.state.rolling ? "Spinning..." : "Spin"}
</button>
</div>
);
}
}
export default Slots;
import React, { Component } from 'react'
class Contents extends Component {
Fruits = ["?", "?", "?", "?", "?", "?"];
render() {
return(
<div className="emptys">
{this.props.roll}
</div>
)
}
}
export default Contents