On the click of a button. I am dispatching the id to my resellerSlice so that I can be able to use .find() and select the exact array object that has the same id as my id
const handleDetailsModal = (e) => {
setResellersId(e.currentTarget.id)
dispatch(uiAction.toggle())
dispatch(
toggleResellers (resellersId)
)
}
On getting to the resellerSlice
const resellerSlice = createSlice({
name: 'reseller',
initialState,
reducers: {
toggleResellers(state, action) {
const resellerId = action.payload;
const clickedReseller = state.resellers?.find((reseller) => reseller.id === resellerId);
console.log(clickedReseller) //returns undefined(this is the problem)
}
}
})
What am I doing wrong?
I need the const 'clickedReseller' to contain the properties of object that its Id is the same as the 'resellersId', So I can use the object.
Here's my initial state
const initialState = [
{
resellerName: 'xxxx xxxx',
id: 1,
resellerLimits: [
{
USD: 700,
GBP: 200,
EURO: 990,
}
],
paymentMethods: [
'wirePay',
'flux',
'bankTransfer'
],
totalTransactions: 0,
rating: 0,
},
This is what the objects inside the array looks like
resellerscontain? And did you check if the valueaction.payloadarrives properly? And what type is it?state.resellersis not empty?findreturns undefined when no values satisfy the condition developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…