React learning (5) Redux

For the demo directly made here, the Action is still unclear.
First , I will summarize the general process:
Insert picture description here
first understand that the translation of store means the meaning of a warehouse. Secondly, we derive createStore from redux, and then introduce the reducer. Remember the picture above, imagine it as In libraries, when looking for books, you need to go to the administrator reducer to get the data. The role of the reducer is to expose a method to return your state, because the reducer manages the state.
Insert picture description here
Insert picture description here
Insert picture description here
This is the output of store.getState().
Insert picture description here
I want to use the redux plugin to go. See need to add this sentence

const store = createStore(
    reducer,
    window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())

OKOKOKOKOKOKOKOKOK, I just watched the technical fat video of station b, and I was stunned when he finished talking. . . . Just do it directly, and rarely say why. . No way, I guess! I like doing this best.

Be a little sticky, go through the process again, I don’t believe it

First add an onChange event to the input, let him get the value of each input, here we carry out the first step Action of the flowchart, we give the action a name, which is equivalent to the key, find it when it is called, and then put it The required value is passed, why, because our overall purpose is to read it out from the warehouse, and then display it. At this time, we must first push the action through store.dispatch, and the push will be triggered every time the value changes.
Insert picture description here
(Here is reducer). There are two parameters here, state and action. State has default values. The operation here is that if a value is entered in the input box of the component, I will push the action in. If the type meets the requirements, I will Assign the value entered by the keyboard to the value I want to display, newstate.inputValue, and then return the newState (actually it feels like state, but the cloned step here, I checked it out, in order to keep the history of each change, there are many One way)
Insert picture description here
This is the content of the action
Insert picture description here
ok. Let’s go back to the component. The newState returned just now is in the store. At this point, let’s think about which line is left. Only store points to the component. Let’s To update the state of the component by subscribing in the component (because we subscribed once when we initialized), as follows:
Insert picture description here
When the value returned by our store from the reducer changes, then we trigger the storeChange function, Of course, this is defined by ourselves, and its name is also very good, concise and clear, at
Insert picture description here
this point, we finally sorted out. . . . I miss vuex a bit, but if I use switch, I feel that react is quite suitable for large-scale projects. If you develop together, you can get everything you need from here. It feels like pushing, taking, and updating the state of the component. Keep working hard. !

Add an add event

    add = () => {
    
    
        const action = {
    
    
            type:'addItem',
        }
        store.dispatch(action)
    }
export default (state = defaultState, action) => {
    
    
    console.log(state, action)
    //Reducer里只能接受state,不能改变state
    if(action.type === 'changeInput'){
    
    
        let newState = JSON.parse(JSON.stringify(state))
        newState.inputValue = action.value
        return newState
    }
    if(action.type === 'addItem'){
    
    
        let newState = JSON.parse(JSON.stringify(state))
        newState.list.push(newState.inputValue)
        newState.inputValue = ''
        return newState
    }
    return state
}

Ok, the last one.
What needs to be noted here is that the parameters cannot be passed directly, it will be called immediately after passing directly. Use bind to fix this.
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46013619/article/details/104921520