我的反应日期日期范围选择器在单击时不起作用/渲染



我正在构建一个 react redux 费用跟踪应用程序,并使用 moment.js 和反应日期来设置时间和过滤时间范围。但是,当我选择任何地方或"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)

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

减速器/过滤器:

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

以下是选择器/费用.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
        }
    })
}

以下是组件/费用列表筛选器.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"来清除日期,以便显示我的演示数据,或选择要过滤的特定范围。我该如何解决这个问题?

因为错误读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工具

嗨,

我现在解决了我的问题:我忘了包括...在我的化简器/过滤器中的状态:

        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
            }

相关内容

  • 没有找到相关文章

最新更新