Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

javascript - this.setState无法正常工作; 子道具继承错误的信息-React-Native(this.setState not working properly; child props inherit wrong information - React-Native)

I am relatively new to React Native and I am trying to create a custom Calendar and I am running into some problems regarding this.setState.(我是React Native的新手,我试图创建一个自定义的Calendar,并且遇到有关this.setState的一些问题。)

Basically when I have the code this.setState in the componentDidMount() function and it prints the right number;(基本上,当我在componentDidMount()函数中有代码this.setState时,它会打印正确的数字;) However, when I did console.log in my Calendar class, it says that my month is 0 (innitial value).(但是,当我在Calendar类中进行console.log时,它表示我的月份为0(初始值)。) I have tried:(我试过了:) having a function in the constructor (ie. this.updateCurrentCalendar()) and ran that in the constructor (returned unmounted error)(在构造函数中具有一个函数(即this.updateCurrentCalendar())并在构造函数中运行该函数(返回卸载错误)) ComponentDidMount() before render (current version)(渲染之前的ComponentDidMount()(当前版本)) Here is my CalendarPage code:(这是我的CalendarPage代码:) componentDidMount(){ var tMonth = parseInt(new Date().getMonth()+1); var tYear = parseInt(new Date().getFullYear()); //ran ,()=>console.log after the first argument in this.setState and returns the correct month this.setState({trackMonth: tMonth, trackYear: tYear}); } render(){ return( <View style = {styles.CalendarViewStyle}> <Calendar style = {styles.CalendarStyle} thisMonth = {this.state.trackMonth} thisYear = {this.trackYear}/> <Button title = "Next" onPress = {()=>this.setState({trackMonth: this.state.trackMonth+1})}/> <Button title = "Back" onPress = {()=>this.setState({trackMonth: this.state.trackMonth-1})}/> </View> ) } And Here is my Calendar code:(这是我的日历代码:) componentDidMount(){ //console.log(this.state.monthStartDay) says it is 0 here* //finding the maximum day in a month var maxDay = this.findMaxDate(this.state.monthDate); this.setState({maxDate: maxDay}); //finding which day does the first day of the path starts var startDay = this.findDay(1); this.setState({monthStartDay: startDay}); //first week start day (if first day of the month does not start on monday) var fLineStartDate = this.findFirstLineStartDate(); this.setState({firstLineStartDate: fLineStartDate}); //second week start day var sLineStartDate = this.findSecondStartLine(fLineStartDate+7); this.setState({secondLineStartDate: sLineStartDate}); } render(){ var lastMaxDay = this.findMaxDate(this.state.monthDate-1); return( <View style = {styles.CalendarViewStyle}> <CalendarLine style = {styles.CalendarLineStyle} item = {{lineDay: this.state.firstLineStartDate, maxDay: this.state.maxDate, lineNum: 0, lastMaxDay: lastMaxDay}}/> </View> Thank you very much for the help!(非常感谢你的帮助!) EDIT: This is my constructor in my Calendar class(编辑:这是我的Calendar类中的构造函数) constructor(props){ super(props); this.state = { monthStartDay: 0, firstLineStartDate: 0, secondLineStartDate: 0, monthDate: this.props.thisMonth, yearDate: this.props.thisYear, maxDate:0, }; this.findDay = this.findDay.bind(this); this.findMaxDate = this.findMaxDate.bind(this); this.findLeapYear = this.findLeapYear.bind(this); this.findFirstLineStartDate = this.findFirstLineStartDate.bind(this); this.findSecondStartLine = this.findSecondStartLine.bind(this); }   ask by Tan Saint translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why not use the props passed to the Calendar class without assigning them to your state constructor(为什么不使用传递给Calendar类的道具而不将它们分配给状态构造函数)

var lastMaxDay = this.findMaxDate(this.state.monthDate-1);(var lastMaxDay = this.findMaxDate(this.state.monthDate-1);) rather use:(宁可使用:) var lastMaxDay = this.findMaxDate(this.props.monthDate-1);(var lastMaxDay = this.findMaxDate(this.props.monthDate-1);) As you are receiving these as props, I am assuming you are not going to be calling setState on them from within the Calendar class?(当您收到这些作为道具时,我假设您不会从Calendar类中对其调用setState吗?) By assigning them to state in the calendar class you are trying to setState in 2 different classes with one setState call.(通过将它们分配给日历类中的状态,您尝试通过一个setState调用将setState分为2个不同的类。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...