so we always need a local state to store the data first
No, I wouldn’t say that. In this case, that makes the most sense because we want a controlled input. I would say that in my coding when using redux, I probably use local state less that 5% of the time, just when I need it. (But that percentage could vary widely depending on the type of app.)
… Is there any way to avoid all local state?
Sure, we could store that in the redux store, too. I don’t think that’s the best idea, but it would work just fine, if we created the action creators and reducer we needed.
This was actually and interview question I had to answer the day before yesterday. (I must had done well - I got a job offer.) “When using redux, when would you want to use something on local (component) state?” I think things that only the component needs to know about. Does the entire app need to know about the input that is local to only this one component? Almost certainly not. On the other hand, does anywhere in the rest of the app need to know about the messages array? That’s hard to say without seeing the rest of the app, but that is a realistic possibility, especially since if I was building this app, I would have had the input in a different component than the output - things would get a little messy without redux - doable but messy. Also, we probably want that messages array to persist even if this component unmounts (e.g., from navigating to another part of the app).
I think my answer to the interview was something along the lines of “If it’s something the rest of the app needs to know about or that needs to be persisted, then it should be in redux. If it is about the internal business of that component, it should be in component state.”
then we use MDTP to access that data and process through action creator.
I’m not sure I understand that statement. I think this would have been clearer if this app had been split into two components - one called Input that uses MDTP and once called List that uses MSTP.
MDPT takes in our action creators and creates a function that will dispatch an action object that will be created by the action creator. This gets placed on the component props by the HOC returned by connect so that it is available within the component.
MSTP will just read things off the redux store and map them to an object that will get merged with the component props, using the same HOC returned by connect.
When you submit, your dispatching function (submitNewMessage, provided to the component by MDTP and connect) will set off a chain of events that will cause the reducer to update the redux store. This will cause MSTP to fire that will provide the updated props to your component which will trigger a rerender.