从操作负载更新 redux lwc 中的状态切片

发布于 2025-01-16 02:51:34 字数 2021 浏览 0 评论 0原文

下面是我更新 state.customer 切片的代码。但是,当它运行时,我发现下一个状态不会使用从操作分派的有效负载进行更新。有人可以指出我缺少什么吗?

减速器:

const initialState1 = {"customer":{}};

const customer = (state = initialState1, action) => {
    console.log('state:', state);
    switch (action.type) {
        case 'INIT_CUSTOMER_INFO':
            return {
               ...state,
               customer: action.payload
            }
        case 'UPD_CUSTOMER_INFO':
            console.log(action);
            return { ...customer, firstname: action.firstname }
        default: return state;
    }
}

export default customer;

来自 LWC 的动作

export const initCustomer = customer => {
    return {
          type: 'INIT_CUSTOMER_INFO',
          payload : customer
    }
}

调度

import { LightningElement, track } from 'lwc';
import { connect } from 'c/connect';
import { updateCustomer, initCustomer } from 'c/actions';
import getJSONData from '@salesforce/apex/RGClass.getCartSummary';

const mapStateToProps = (state, ownProps) => ({
    customer: state.customer
})

const mapDispatchToProps = (dispatch, ownProps) => ({
    initCustomer : customer => dispatch(initCustomer(customer)),
    updateCustomer : customer => dispatch(updateCustomer(customer))
})

export default class DigiForm extends LightningElement {
@track firstname;
showfirstname;

connectedCallback() {
    //add the hook
    connect(mapStateToProps, mapDispatchToProps)(this);//connects the
    //api call
    getJSONData()
        .then(result => {
            console.log('result:',result);
            this.initCustomer(result);
        })
        .catch(error => {
            console.log(error);
        });
}
onContinue = () => {
    let fn = this.template.querySelector('lightning-input').value;

    console.log('firstname:', fn);
    this.updateCustomer({ firstname : fn});
    if(fn != null){
        this.showfirstname = true;
    }
}
}

尝试了几个选项,但没有奏效。任何帮助将不胜感激!

below is my code to update the state.customer slice. However when this runs, I see that the next state is not updated with the payload dispatched from the action. can some one please point out what I'm missing?

reducer:

const initialState1 = {"customer":{}};

const customer = (state = initialState1, action) => {
    console.log('state:', state);
    switch (action.type) {
        case 'INIT_CUSTOMER_INFO':
            return {
               ...state,
               customer: action.payload
            }
        case 'UPD_CUSTOMER_INFO':
            console.log(action);
            return { ...customer, firstname: action.firstname }
        default: return state;
    }
}

export default customer;

action

export const initCustomer = customer => {
    return {
          type: 'INIT_CUSTOMER_INFO',
          payload : customer
    }
}

dispatch from LWC

import { LightningElement, track } from 'lwc';
import { connect } from 'c/connect';
import { updateCustomer, initCustomer } from 'c/actions';
import getJSONData from '@salesforce/apex/RGClass.getCartSummary';

const mapStateToProps = (state, ownProps) => ({
    customer: state.customer
})

const mapDispatchToProps = (dispatch, ownProps) => ({
    initCustomer : customer => dispatch(initCustomer(customer)),
    updateCustomer : customer => dispatch(updateCustomer(customer))
})

export default class DigiForm extends LightningElement {
@track firstname;
showfirstname;

connectedCallback() {
    //add the hook
    connect(mapStateToProps, mapDispatchToProps)(this);//connects the
    //api call
    getJSONData()
        .then(result => {
            console.log('result:',result);
            this.initCustomer(result);
        })
        .catch(error => {
            console.log(error);
        });
}
onContinue = () => {
    let fn = this.template.querySelector('lightning-input').value;

    console.log('firstname:', fn);
    this.updateCustomer({ firstname : fn});
    if(fn != null){
        this.showfirstname = true;
    }
}
}

Tried a couple of options but none worked. any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文