Redux useSelector returns 未定义嵌套值
Redux useSelector returns undefined for nested value
我创建了一个客户类型
export interface ICustomer {
customer_code: string;
name: string;
}
并将其导入我的减速器并在此文件中配置我的初始状态
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import {
ICustomer,
} from "../types/createInvoice_types";
const InvoiceCustomerState: ICustomer[] = [];
export const createInvoiceCustomerSlice = createSlice({
name: "CustomerToInvoice",
initialState: invoiceDetailsState,
reducers: {
create: {
reducer: (state, { payload }: PayloadAction<ICustomer>) => {
state.push(payload);
},
prepare: (customer: Partial<ICustomer>) => ({
payload: customer,
}),
},
remove: (state) => {
return InvoiceCustomerState;
},
},
});
export const {
create: addCustomerToInvoice,
remove: deleteCustomerToInvoice,
} = createInvoiceCustomerSlice.actions;
在我想使用它的组件中
import {
useSelector,
RootStateOrAny,
} from "react-redux";
const customer = useSelector(
(state: RootStateOrAny) => state.customerToInvoice
);
当我 console.log(customer)
我得到 [{"code": "123", "name": "Cash Customer"}]
但是当我尝试 console.log(customer.name)
我得到 undefined
我也尝试过这个,但我仍然无法定义
interface IRootState {
customer_code: string;
name: string;
}
const customer3 = useSelector((state: IRootState) => {
state.customerToInvoice.name;
});
console.log(customer3);
谁能指出我做错了什么
when i console.log(customer) i get [{"code": "123", "name": "Cash Customer"}]
but when i try console.log(customer.name) i get undefined
答案就在这两句话中。
当你console.log(customer)
时,清楚地表明customer === [{"code": "123", "name": "Cash Customer"}]
,它是一个包含单个对象的数组。
现在如果你想打印第一个元素的名称,你应该 console.log(customer[0].name)
而不是 console.log(customer.name)
。
我创建了一个客户类型
export interface ICustomer {
customer_code: string;
name: string;
}
并将其导入我的减速器并在此文件中配置我的初始状态
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import {
ICustomer,
} from "../types/createInvoice_types";
const InvoiceCustomerState: ICustomer[] = [];
export const createInvoiceCustomerSlice = createSlice({
name: "CustomerToInvoice",
initialState: invoiceDetailsState,
reducers: {
create: {
reducer: (state, { payload }: PayloadAction<ICustomer>) => {
state.push(payload);
},
prepare: (customer: Partial<ICustomer>) => ({
payload: customer,
}),
},
remove: (state) => {
return InvoiceCustomerState;
},
},
});
export const {
create: addCustomerToInvoice,
remove: deleteCustomerToInvoice,
} = createInvoiceCustomerSlice.actions;
在我想使用它的组件中
import {
useSelector,
RootStateOrAny,
} from "react-redux";
const customer = useSelector(
(state: RootStateOrAny) => state.customerToInvoice
);
当我 console.log(customer)
我得到 [{"code": "123", "name": "Cash Customer"}]
但是当我尝试 console.log(customer.name)
我得到 undefined
我也尝试过这个,但我仍然无法定义
interface IRootState {
customer_code: string;
name: string;
}
const customer3 = useSelector((state: IRootState) => {
state.customerToInvoice.name;
});
console.log(customer3);
谁能指出我做错了什么
when i console.log(customer) i get [{"code": "123", "name": "Cash Customer"}]
but when i try console.log(customer.name) i get undefined
答案就在这两句话中。
当你console.log(customer)
时,清楚地表明customer === [{"code": "123", "name": "Cash Customer"}]
,它是一个包含单个对象的数组。
现在如果你想打印第一个元素的名称,你应该 console.log(customer[0].name)
而不是 console.log(customer.name)
。