0

Столкнулся с неожиданной проблемой. Есть компонент с формой и кнопкой:

<PrimaryButton onClick={this._handleSubmit} text="Add" />

При нажатии вызывает:

    _handleSubmit = () => {

        // Проблема тут
        this.props.loadCustomersAction()

        var self = this
        axiosWithProgress.post(
            '...AddCustomer.php',
            { name: this.state.name, mail: this.state.mail },
            { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
        ).then((response) => {
            if (response.data.error) self.setState({ error: response.data.error })
            else self.setState({ error: undefined })
            if (response.data.success === true) {
                self.setState({ hideDialog: true })
            }
        })
    }

Вызываемая функция:

const mapDispatchToProps = dispatch => {
    return {
        loadCustomersAction: () => dispatch(loadCustomers())
    }
}

Redux экшн:

export function loadCustomers() {
  return (dispatch) => {
    axiosWithProgress.get(`...LoadCustomers.php`)
      .then(r => {
        const customers = r.data.customers
        dispatch({
          type: 'LOAD_CUSTOMERS',
          customers: customers
        })
      })
  }
}

Но все это не работает и выдает ошибку:

TypeError: _this.props.loadCustomersAction is not a function

Аналогичный механизм в другом компоненте работает отлично. Перепробовал множество вариантов из первых строк поисковика.

1 ответ 1

0

Попробуй так:

const mapDispatchToProps = dispatch => ({
    return {
        loadCustomersAction: () => dispatch(loadCustomers())
    }
})

Скобки забыл: const mapDispatchToProps = dispatch => ({});

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.