TypeError: Cannot assign to read only property '' of object '#<Object>' Redux Tool Kit Slice for Firebase Storage REACT NATIVE

Viewed 40

Hello I am trying to set my downloadUrl after uploading to firebase storage and I can't seem to get it to work with Redux Toolkit. I have a solution but I would rather have it the correct way but I cant seem to figure out it would love to get some feed back

Here is the Documentation example. I understand that numbers work differently than objects but if they can change it like this why cant I?

    incrementByAmount: (state, action) => {
  state.value += action.payload
},

And here is my code:

setImageUrl: (state, action: PayloadAction<string>) => {
  state.imageUrl = action.payload;
},

And here is where I call it


    const downloadUrl = await FirebaseStorageService.uploadFile(
      photo,
      setUploadProgress
    );

    dispatch(setImageUrl(downloadUrl));

Here is the error

ossible Unhandled Promise Rejection (id: 5):
TypeError: Cannot assign to read only property 'imageUrl' of object '#<Object>'
TypeError: Cannot assign to read only property 'imageUrl' of object '#<Object>'

Thank you in advance I have seen similar questions on other places but I feel like the answers are not clear. Can I change properties on an object directly or not with RTK?

1 Answers

I solved it but I don't think it is the right resolution or rather it does not really explain RTK to me coming from .Net. I just think it is incorrect:

  const downloadUrl = await FirebaseStorageService.uploadFile(
                        photo,
                        setUploadProgress
                      );
  reduxExpense.imageUrl = downloadUrl;
  dispatch(setExpense(reduxExpense));

I don't think we need redux instead I could just do:

expense.ImageUrl = downloadUrl; 

Or is this a correct implementation? Could someone help? Thanks!

Related