单击时我的反应日期 DateRangePicker 不是 working/rendering

My react-dates DateRangePicker is not working/rendering when clicked

我正在构建一个 React redux 费用跟踪应用程序并使用 moment.js 和 react-dates 来设置时间和过滤时间范围。但是,当我在任何地方 select 或 "x" 清除日期时出现错误,然后屏幕上没有任何内容。

expenses.js:9 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
    at expenses.js:9
    at Array.filter (<anonymous>)
    at expenses.js:5
    at Function.mapStateToProps [as mapToProps] (ExpenseList.js:17)
    at mapToPropsProxy (wrapMapToProps.js:41)
    at handleNewState (selectorFactory.js:44)
    at handleSubsequentCalls (selectorFactory.js:58)
    at pureFinalPropsSelector (selectorFactory.js:63)
    at Subscription.checkForUpdates [as onStateChange] (connectAdvanced.js:247)
    at Subscription.handleChangeWrapper (Subscription.js:70)

我已经仔细检查过,但仍然找不到问题所在...

reducers/filters:

const filterReducerDefaultState = {
    text: '',
    sortBy: 'date',
    startDate: moment().startOf('month'),
    endDate: moment().endOf('month')
}

这里是 selectors/expenses.js:

import moment from 'moment'

//Get visible expenses
export default (expenses, { text, sortBy, startDate, endDate }) => {
    return expenses.filter((expense) => {
        const createdAtMoment = moment(expense.createdAt)
        const startDateMatch = startDate ? startDate.isSameOrBefore(createdAtMoment, 'day') : true
        const endDateMatch = endDate? endDate.isSameOrAfter(createdAtMoment, 'day'): true
        const textMatch = expense.description.toLowerCase().includes(text.toLowerCase())

        return startDateMatch && endDateMatch && textMatch
    }).sort((a, b) => {
        if (sortBy === 'date') {
            return a.createdAt < b.createdAt ? 1 : -1
        } else if (sortBy === 'amount') {
            return a.amount < b.amount ?  1 : -1
        }
    })
}

这里是 components/ExpenseListFilters.js:

import React from 'react'
import { connect } from 'react-redux'
import { DateRangePicker } from 'react-dates'
import { setTextFilter, sortByAmount, sortByDate, setStartDate, setEndDate } from '../actions/filters'

class ExpenseListFilters extends React.Component{
    state={
        calendarFocused: null
    }
    onDatesChange = ({startDate, endDate}) => {
        this.props.dispatch(setStartDate(startDate))
        this.props.dispatch(setEndDate(endDate))
    }
    onFocusChange = (calendarFocused) => {
        this.setState(() => ({ calendarFocused}))
    }
    render(){
        return (
            <div>
        <input 
        type="text" 
        value={this.props.filters.text} 
        onChange={(e) => {
            this.props.dispatch(setTextFilter(e.target.value))
        }} />
        <select 
        value ={this.props.filters.sortBy}
        onChange={(e)=> {
            if (e.target.value === 'date'){
                this.props.dispatch(sortByDate())
            } else if (e.target.value === 'amount') {
                this.props.dispatch(sortByAmount())
            }
        }}>
            <option value="date">Date</option>
            <option value="amount">Amount</option>
        </select>
        <DateRangePicker 
        startDate={this.props.filters.startDate}
        startDateId='startDateId'
        endDate={this.props.filters.endDate}
        endDateId='endDateId'
        onDatesChange={this.onDatesChange}
        focusedInput={this.state.calendarFocused}
        onFocusChange={this.onFocusChange}
        showClearDates={true}
        numberOfMonths={1}
        isOutsideRange={()=> false}
        />
    </div>
        )
    }
}

const mapStateToProps = (state) => {
    return {
        filters: state.filters
    }
}
export default connect(mapStateToProps)(ExpenseListFilters)

我希望能够通过在日历上单击 "X" 来清除日期,以便显示我的演示数据,或者 select 过滤特定范围。 我该如何解决?

因为错误显示为 Cannot read property 'toLowerCase' of undefined,您的问题出在代码 const textMatch = expense.description.toLowerCase().includes(text.toLowerCase())

为了防止出现此类问题,我喜欢包含一个默认值,如下所示

export default (expenses, { text = '', sortBy, startDate, endDate }) => {
    return expenses.filter((expense = {}) => {
        const { description = '' } = expense
        const createdAtMoment = moment(expense.createdAt)
        const startDateMatch = startDate ? startDate.isSameOrBefore(createdAtMoment, 'day') : true
        const endDateMatch = endDate? endDate.isSameOrAfter(createdAtMoment, 'day'): true
        const textMatch = expense.description.toLowerCase().includes(text.toLowerCase())

        return startDateMatch && endDateMatch && textMatch
    }).sort((a, b) => {

通过为文本、费用和描述添加默认值,您的代码不会出错 - 但问题可能不会得到解决。

为了解决这个问题,我建议将控制台日志添加到每一行,以确保值符合您的预期 - 或者使用 nodejs 的内置 debugger 工具

嗨,我现在解决了我的问题:我忘记在 :

中的 reducers/filters 中包含 ...state
        case 'SET_START_DATE':
            return {
                startDate: action.startDate
            }
        case 'SET_END_DATE':
            return {
                endDate: action.endDate
            }

应该是:

        case 'SET_START_DATE':
            return {
                ...state,
                startDate: action.startDate
            }
        case 'SET_END_DATE':
            return {
                ...state,
                endDate: action.endDate
            }