Ich lerne React und ich erstelle eine Anwendung für mich, um mein Studium zu verfolgen. Alles funktioniert gut.Wie setze ich in meinem Fall Requisiten für den Zustand in der Komponente?
In meiner Anwendung habe ich eine klassenbasierte Komponente, in der ich Requisiten übergeben muss. Ich habe jedoch gelernt, dass ich niemals Requisiten in einer klassenbasierten Komponente weitergeben sollte, weil ich den Zustand irgendwo ändern könnte. Aber ich weiß nicht, wie ich meinen Code funktionieren lassen kann, ohne Requisiten zu übergeben, so wie ich es tue. Ich selbst studiere, also habe ich keinen Lehrer zu fragen, daher ist StackOverflow mein bester Tutor. Vielen Dank im Voraus für Ihre Hilfe.
Dies ist mein Code.Ich habe ihn entsprechend in eine Antwort geändert und es funktioniert jetzt gut, aber gibt es irgendetwas, das ich in meinem Code ändern oder vermeiden sollte?
import React from 'react';
import SubjectFrom from './SubjectForm';
import {startSubject} from '../actions/subjectAction';
import {connect} from 'react-redux';
class BeginSubject extends React.Component{
constructor(props){
super(props);
this.state={
timeRun:0,
onFinish:'',
buttonChange:'start',
timer:null
}
}
componentWillMount(){
this.setState({timeRun:this.props.subject?this.props.subject.hour*60*60+this.props.subject.minute*60:0,});
}
componentWillUnmount(){
let hour = Math.floor(this.state.timeRun/(60 * 60));
let minute= Math.floor((this.state.timeRun % (60 * 60))/60);
this.onPause();
if(this.props.subject){
this.props.dispatch(startSubject(this.props.subject.id,{hour,minute}))
console.log(this.props.subject.id)
}
}
onStart=()=>{
clearInterval(this.timer)
this.timer = setInterval(()=>{
let count=this.state.timeRun
count--
if(count<0){
this.setState({onFinish:'well Done'})
clearInterval(this.timer);
return;
}
let hourleft = Math.floor(count/(60 * 60));
let minuteleft = Math.floor((count % (60 * 60))/60);
let secondleft = count % 60;
this.setState({onFinish:`you have ${hourleft} hour ${minuteleft} minute and ${secondleft} second until reaching your goal`});
this.setState({timeRun:count})
},1000)
}
onPause=()=>{
clearInterval(this.timer)
let time = this.props.subject?this.props.subject.hour*60*60+this.props.subject.minute*60:0;
if(this.state.timeRun<time){
this.setState({buttonChange:'resume'})
return;
}
}
onReset=()=>{
const resetConfirm=confirm('you have not finished, do you want to reset?')
if(resetConfirm===true){
clearInterval(this.timer)
this.setState({timeRun:this.props.subject?this.props.subject.hour*60*60+this.props.subject.minute*60:0,
onFinish:'',
buttonChange:'start',
timer:null})
}
}
render(){
return(
<div>
<p>{this.props.subject?this.props.subject.subjectName:'there is nothing to do for now'}</p>
<p>{this.props.subject?`you have ${this.props.subject.hour} hour and ${this.props.subject.minute} minute to work`:'there is no time set'}</p>
<button onClick={this.onStart}>{this.state.buttonChange}</button>
<button onClick={this.onPause}>pause</button>
<button onClick={this.onReset}>reset</button>
<p>{this.state.onFinish}</p>
</div>
)
}
};
const mapStateToProps=(state,props)=>{
return{
subject:state.subjects.find((subject)=>subject.id===props.match.params.id)
};
}
export default connect(mapStateToProps)(BeginSubject)
Yeah doing so wird machen, was auch immer den Zustand unveränderbar machen –
bei der Verwendung von Requisiten, müssen Sie Handler zur Verfügung stellen, um die Requisiten zu modifizieren –