Reactjs 无法将值传递给React.Js中的父组件
我正在为那件事绞尽脑汁 我正在尝试使用react calendar包构建日历。在我的应用程序的一个地方,我需要访问日历月的值。我正在调用activedTechRange上的components属性,我在组件的文档(react calendar)中找到了该属性。这确实是一个回调,所以我尝试将其用作提取月份值并将其发送到父组件的机会。但这不起作用,不仅值没有按预期更改,而且日历停止工作。你知道是什么引起的吗?我也尝试过在回调中设置状态,但结果相同,值不正确 以下是我的代码块:Reactjs 无法将值传递给React.Js中的父组件,reactjs,redux,Reactjs,Redux,我正在为那件事绞尽脑汁 我正在尝试使用react calendar包构建日历。在我的应用程序的一个地方,我需要访问日历月的值。我正在调用activedTechRange上的components属性,我在组件的文档(react calendar)中找到了该属性。这确实是一个回调,所以我尝试将其用作提取月份值并将其发送到父组件的机会。但这不起作用,不仅值没有按预期更改,而且日历停止工作。你知道是什么引起的吗?我也尝试过在回调中设置状态,但结果相同,值不正确 以下是我的代码块: import Reac
import React, { Component } from 'react';
import Calendar from 'react-calendar';
import ChooseHour from './ChooseHour';
import { connect } from 'react-redux';
import * as actions from '../actions';
class Calendario extends Component {
state = { showHours: false,}
onChange = date => this.setState({
date }, () => {
const { chosenRoom } = this.props;
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const fullDate = `${year}/${month}/${day}`;
const roomAndDayObj = {fullDate, chosenRoom};
this.props.sendRoomAndDay(roomAndDayObj);
}
)
onClickDay(e) {
this.setState({ showHours: true });
}
passActiveDate(activeDate) {
const monthAfterPress = activeDate.getMonth() + 1;
console.log(monthAfterPress);
// this is weird in my opinion, I can log it and it works as expected,
// when I'm firing the next line the values are incorrect
// this.props.activeMonthToPass(monthAfterPress); //this line makes a problem
}
render() {
const { booked } = this.props;
return (
<div>
<div className="calendarsCont">
<Calendar
onChange={this.onChange}
onClickDay={(e) => this.onClickDay(e)}
onActiveDateChange={({ activeStartDate }) => this.passActiveDate(activeStartDate)}
value={this.state.date}
locale="pl-PL"
tileDisabled={({date, view }) =>
date.getDate()===15 && date.getMonth()===6 && date.getFullYear()===2018}
/>
</div>
<div>
{this.state.showHours ?
<ChooseHour chosenDay={this.state.date} chosenRoom={this.props.chosenRoom}/> :
null}
</div>
</div>
)
}
}
export default connect (null, actions)(Calendario);
import React,{Component}来自'React';
从“反应日历”导入日历;
从“/ChooseHour”导入ChooseHour;
从'react redux'导入{connect};
将*作为动作从“../actions”导入;
类Calendario扩展组件{
状态={showHours:false,}
onChange=date=>this.setState({
日期},()=>{
const{chosenRoom}=this.props;
const year=date.getFullYear();
const month=date.getMonth()+1;
const day=date.getDate();
const fullDate=`${year}/${month}/${day}`;
const roomAndDayObj={fullDate,chosenRoom};
this.props.sendRoomAndDay(roomAndDayObj);
}
)
一天(e){
this.setState({showHours:true});
}
passActiveDate(活动日期){
const monthAfterPress=activeDate.getMonth()+1;
控制台日志(monthAfterPress);
//这在我看来很奇怪,我可以记录它,它可以按预期工作,
//当我发射下一行时,值是不正确的
//this.props.activeMonthToPass(monthAfterPress);//这行有问题
}
render(){
const{booked}=this.props;
报税表(
本.onClickDay(e)}
OnActivatedTechRange={({activeStartDate})=>this.passActivateDate(activeStartDate)}
值={this.state.date}
locale=“pl”
tileDisabled={({date,view})=>
date.getDate()==15&&date.getMonth()==6&&date.getFullYear()==2018}
/>
{this.state.showHours?
:
空}
)
}
}
导出默认连接(空,操作)(Calendario);
为什么您既需要onChange
又需要onactivatedtechange
?为什么不直接使用onChange
?Hi@azium,因为我需要在用户单击day互动程序之前获得访问权限。OnSumbit在单击后工作。好的,它仍然不清楚问题是什么。。你是说,this.props.activeMonthToPass(…)
是什么破坏了它?如果没有这个函数调用,事情仍然可以运行?这个.props.activeMonthToPass(monthAfterPress)破坏了代码,即月份没有改变,同样的问题可以通过这个.setState({x:monthAfterPress})而不是上面的行重现。但是…console.log(monthAterPress)像一个符咒一样工作(我可以看到几个月的变化)当控制台崩溃时,你会发现任何错误吗?调用setState
不应该像那样突然中断任何事情