私はこの権利を語ったかどうか分かりません。基本的には、機能するカウンタ(インクリメントまたはデクリメント)であるコンポーネントがあります。他のコンポーネントは、(デフォルトでは)25から0にカウントダウンするタイマーです。州の州からプロパティの値を借りて返す
これまではタイマーを25に設定していましたが、私はの値としてタイマーを変更しようとしています。カウンタがに変更され、使用開始ボタンが押されるとカウンタはで設定された数からカウントダウンします。
コンポーネントは個別に動作させることはできますが、一緒に動作させることはできません。 私はthis.state.currentCount
をthis.props.time
の値に設定してから、this.state.currentCount
の値を変更しようとしましたが、運はありません。タイマーが変化しないか、カウンターの値が反映されません。
代わりにcomponentWillReceiveProps
を使用する必要があるかどうかわかりません。
ご協力いただければ幸いです。それがまったく役に立ったら、一番下のスクリーンショットがあります。
セッションコンポーネント:
const Session = React.createClass({
getInitialState: function() {
return {
minutes: 25,
seconds: 0
};
},
increment: function() {
this.setState({ minutes: this.state.minutes + 1 });
},
decrement: function() {
this.setState({ minutes: this.state.minutes - 1 });
},
timeToString: function(time) {
return time + ':00';
},
render: function() {
return (
<section>
<button onClick={this.increment}>+</button>
<button onClick={this.decrement}>-</button>
{this.state.minutes}
<Clock time={this.state.minutes}/>
</section>
);
}
});
module.exports = Session;
時計コンポーネント:
const Clock = React.createClass({
getInitialState: function() {
return { currentCount: this.props.time };
},
startTimer: function() {
var intervalId = setInterval(this.timer, 1000);
this.setState({ intervalId: intervalId });
},
pauseTimer: function() {
clearInterval(this.state.intervalId);
this.setState({ intervalId: this.props.time });
},
timer: function() {
var newCount = this.state.currentCount - 1;
if (newCount >= 0) {
this.setState({ currentCount: newCount });
} else {
clearInterval(this.state.intervalId);
}
},
render: function() {
return (
<section>
<button onClick={this.startTimer}>Start</button>
<button onClick={this.pauseTimer}>Pause</button>
{this.props.time}
<br></br>
{this.state.currentCount}
</section>
);
}
});
module.exports = Clock;
Js-fiddleは素晴らしいでしょう –
私はそれを理解しようとしています。 JSfiddleで動作する反応のすべての部分を取得することは、必要以上に困難です。 – Jose