简体   繁体   English

redux如何存储数据?

[英]How does redux store data?

I'm working on a little app to learn more about how to use redux. 我正在开发一个小应用程序,以了解有关如何使用redux的更多信息。 From my interpretations of what is written about redux, you can store and update data on the store. 根据我对redux的理解,您可以在商店中存储和更新数据。 In my app, I have a HTML form with two text inputs that when submitted uses an action creator function to handle submission. 在我的应用程序中,我有一个带有两个文本输入的HTML表单,提交时使用动作创建者功能来处理提交。 The creator function then dispatches the data to a reducer function, which is suppose to store the data into the redux store. 然后,creator函数将数据分派给reducer函数,该函数应将数据存储到redux存储中。 When I console.log out the store, there is no data stored in the redux store. 当我console.log注销存储时,redux存储中没有存储任何数据。 Does anyone see where my problem lies or if my interpretation of what redux is suppose to do is wrong? 有人看到我的问题出在哪里吗,或者我对redux的解释是错误的?

index.js index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import reduxThunk from 'redux-thunk';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import rootReducer from './truth/reducers'

const store = createStore(rootReducer(), {}, applyMiddleware(reduxThunk));

console.log(store.getState());

ReactDOM.render(
    <Provider store={store}>
        <App /> 
    </Provider>,
    document.getElementById('root')
);

SimpleForm.js SimpleForm.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux'
import { Field, reduxForm } from 'redux-form';
import { fetchName }from '../truth/actions';

class SimpleForm extends Component {

    render() {
        const { handleSubmit, pristine, reset, submitting, fetchName} = this.props;
        return (
            <form onSubmit={handleSubmit(fetchName)}>
                <br />
                <Field name="fname" type="text" component="input" label="First Name" />
                <Field name="lname" type="text" component="input" label=" Last Name" />
                <div>
                    <button type="submit" disabled={submitting}> Submit </button>
                    <button type="button" disabled={pristine || submitting} onClick={reset}> Reset </button>
                </div>
            </form>
        );
    }
}

const myReduxForm = reduxForm({
    form: "mySimpleForm",
    onSubmit: fetchName,
    onSubmitSuccess:  (result, dispatch) => {
        console.log("I am in the onSubmitSuccess func");
        console.log('i am after dispatch');
    }
})


export default compose(
    connect(null, {fetchName} ),
    myReduxForm
    )(SimpleForm);

actions' index.js file 操作的index.js文件

import { WHOLE_NAME, MY_LIKES} from './types';
import axios from 'axios';

export const fetchName = (values) =>  async dispatch => {
    //const res = axios.post();
    console.log("I am in fetchName");
    console.log(values);

   dispatch({type: WHOLE_NAME, payload: values});
}

nameReducer.js nameReducer.js

import { WHOLE_NAME } from '../actions/types';

const name = {
    fname: "",
    lname: ""
}

export const setNames = (state = name, action) => {
    console.log("I am in setnNames")
    console.log(action.payload);
    let myPayload = {...action.payload};
    console.log(myPayload.fname);  //logs out correctly
    switch (action.type) {
        case WHOLE_NAME:
            return { ...state, fname: myPayload.fname, lname: myPayload.lname }
        default:
            return state;
    }
}

reducers' index.js file 减速器的index.js文件

import { combineReducers } from 'redux';
import { setNames } from './nameReducer';
import { reducer as reduxFormReducer } from 'redux-form';

const rootReducer = () => combineReducers({
    setNames,
    form: reduxFormReducer
});

export default rootReducer;
const store = createStore(rootReducer(), {}, applyMiddleware(reduxThunk));

console.log(store.getState()); //this is called only once before you even render anything 

You can use redux devtools to see what does the store looks like in any particular moment. 您可以使用redux devtools来查看商店在任何特定时刻的外观。 https://github.com/zalmoxisus/redux-devtools-extension https://github.com/zalmoxisus/redux-devtools-extension

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM