gpt4 book ai didi

javascript - redux 是如何存储数据的?

转载 作者:行者123 更新时间:2023-11-30 11:10:59 26 4
gpt4 key购买 nike

我正在开发一个小应用程序以了解有关如何使用 redux 的更多信息。根据我对 redux 的解释,你可以在 store 上存储和更新数据。在我的应用程序中,我有一个带有两个文本输入的 HTML 表单,在提交时使用 Action 创建器函数来处理提交。 creator 函数然后将数据分派(dispatch)给 reducer 函数,该函数假设将数据存储到 redux 存储中。当我 console.log 退出商店时,redux 商店中没有存储任何数据。有没有人看到我的问题出在哪里,或者我对 redux 应该做什么的解释是错误的?

索引.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

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);

Action 的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

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 文件

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

您可以使用 redux devtools 查看商店在任何特定时刻的样子。 https://github.com/zalmoxisus/redux-devtools-extension

关于javascript - redux 是如何存储数据的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53711921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com