InputDateCustom.js
import DatePicker from 'react-toolbox/lib/date_picker/DatePicker'; import React, { Component } from 'react'; const datetime = new Date(2015, 10, 16); const min_datetime = new Date(new Date(datetime).setDate(8)); datetime.setHours(17); datetime.setMinutes(28); export default class InputDateCustomizado extends Component{ state = {date2: datetime}; handleChange = (item, value) => { console.log(item+" - "+value) this.setState({...this.state, [item]: value}); }; render() { return ( DatePicker label = {this.props.label} locale = {localeExample} name = {this.props.name} required = {this.props.required} onChange = {this.handleChange.bind(this, 'date1')} value = {this.state.date1} /> ); } }
其他组件:Cadastro.js
constructor(props) { super(props); this.state = {msg: '', fim_vigencia:'', nome:''} this.setNome = this.setNome.bind(this) this.setFimVigencia = this.setFimVigencia.bind(this) } setFimVigencia(evento){ console.log("date") this.setState({fim_vigencia:evento.target.value}); }
float: right;
clear: both;
答案 0 :(得分:0)
在onChange事件中获取值或使用value
道具。文档示例:http://react-toolbox.com/#/components/date_picker
<DatePicker label='Birthdate' onChange={this.handleChange.bind(this, 'date1')} value={this.state.date1} />
您可以访问handleChange
事件中的值,以便使用当前所选日期更新您的状态。
DatePicker
,现在希望通过DatePicker
组件获取Cadastro.js
值。
您需要在Cadastro.js
中创建一个方法,该方法接受来自InputDateCustomizado
组件的状态更改,然后将该函数作为prop传递给InputDateCustomizado
组件。在状态更改的InputDateCustomizado
中,调用传入的函数,它应该更新父组件中的状态。然后,您将始终在父组件中具有datepicker值。
看起来你几乎就在那里。您需要向updateState
组件添加Cadastro.js
函数。在InputDateCustomizado
组件handleChange
事件中,您需要调用this.props.updateState
并传入新值。
在Cadastro.js
updateState = (data) => {
this.setState({
date: data.data //set your state here to the date
})
}
在InputDateCustomizado
handleChange = (item, value) => {
console.log(item+" - "+value)
this.setState({...this.state, [item]: value});
this.props.updateState(this.state);
};