ReactJS无法使用材料ui读取未定义的属性'handleChange'

时间:2018-01-26 14:11:56

标签: reactjs events datepicker onchange

我正在尝试使用ReactJS动态构建基于props值的搜索栏,但是我得到以下错误'无法读取属性'handleChange'of undefined'。 我不知道我做错了什么,我只想显示两个DatePicker。

这是我的代码:

app.js

import React, { Component } from 'react';
import { render } from 'react-dom';
import SearchBar from '../searchBar/searchBar';

export default class Hello extends Component {

  render() {

    const today = Moment(new Date ()).format('YYYYMMDD');

    const fields_newSec = [ {"type":"dateField", "name":"from", "value":today, "label":"From Date"},
                            {"type":"dateField", "name":"to", "value":today, "label":"To Date"} ];

    return (
          <div>
           <SearchBar searchBarFields={fields_newSec} />
          </div>
    );
  }
}
render(<Hello />, document.getElementById('app'));

searchBar.js

import React from 'react';
import DatePicker from 'material-ui/TextField';
import Moment from 'moment';

class SearchBar extends React.Component {

  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(event) {
    console.log("Event: "+event.target.value);
    console.log("Text Name: "+event.target.name);
  }

  render() {

    var itemsList = this.props.searchBarFields.map(function(element, index) {
        return (
          <DatePicker
                key={index}
                label={element.label}
                type="date"
                name={element.name}
                defaultValue={Moment(new Date()).format('YYYY-MM-DD')}
                onChange={this.handleChange}
                InputLabelProps={{
                  shrink: true,
                }}                
            />
        );
    });

    return(
        <div>{itemsList}</div>
    );     
  }
}
export default SearchBar;

1 个答案:

答案 0 :(得分:1)

我认为你需要使用地图的箭头功能

render() {

return (

<div>
{this.props.searchBarFields.map((element, index) => (

              <DatePicker
                    key={index}
                    label={element.label}
                    type="date"
                    name={element.name}
                    defaultValue={Moment(new Date()).format('YYYY-MM-DD')}
                    onChange={this.handleChange}
                    InputLabelProps={{
                      shrink: true,
                    }}                
                />
       ))}
</div>

)

}