我在从react redux教程中获得了一些注册,登录和主页的信息后,就开始制作react应用程序。我正在使用redux来处理状态,当我在登录页面的输入字段中键入内容时,状态不会更新。
在constants.js中
export const USER_LOGGED_IN = 'USER_LOGGED_IN';
在actions.js
import * as constants from './constants';
export const userLoggedIn = (text) => ({
type: constants.USER_LOGGED_IN,
payload: text
});
在reducer.js中
import * as actionName from "./constants";
const initialStateLoggedIn = {
email: "",
};
export const userLoggedInReducer = ( state = initialStateLoggedIn, action = {} ) => {
switch (action.type) {
case actionName.USER_LOGGED_IN:
return {
...state,
email: action.payload,
};
default:
return state;
}
};
在index.js中
import { createStore, combineReducers, applyMiddleware } from "redux";
import { Provider } from "react-redux";
import thunkMiddleware from "redux-thunk";
import { createLogger } from "redux-logger";
import { userLoggedInReducer } from "./reducers";
import { composeWithDevTools } from "redux-devtools-extension";
const logger = createLogger();
const rootReducers = combineReducers({ userLoggedInReducer });
const store = createStore(
rootReducers,
composeWithDevTools(applyMiddleware(thunkMiddleware, logger))
);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
在SignIn.js中
import { connect } from "react-redux";
import { userLoggedIn } from "./../../actions";
const mapStateToProps = state => {
console.log(`mapStateToProps: ${state.userLoggedInReducer.email}`); //I think this is where it reads the input real time?
return { email: state.userLoggedInReducer.email };
};
const mapDispatchToProps = dispatch => {
return {
onEmailFilled: event =>
dispatch(userLoggedIn(event.target.signin_email.value))
};
};
render() {
const { email, onEmailFilled } = this.props;
console.log(`Hello ${email}`);
return{
<form ......
<InputComponent
id_name="signin_email" //this is the name attribute
input_label="Email"
input_type="email"
function="{onEmailFilled}" //can I pass onEmailFilled like this?
/>
.......
export default connect( mapStateToProps, mapDispatchToProps)(SignIn);
}
在InputComponent.js中
import React, { Component } from "react";
class InputComponent extends Component {
render() {
return (
<div className="measure mt4 mt4-1 mt4-m mt4-ns">
<label htmlFor={this.props.id_name} className="f6 b db mb2">
{this.props.input_label}
{this.props.isOptional ? (
<span className="normal black-60">(optional)</span>
) : null}
</label>
<input
id={this.props.id_name}
name={this.props.id_name}
className="input-reset ba b--black-20 pa2 mb2 db w-100 lh-copy lh-copy-l lh-copy-m lh-copy-ns"
type={this.props.input_type}
autoComplete="on"
onChange={this.props.function} //passed from SignIn.js
/>
</div>
);
}
}
export default InputComponent;
我有redux chrome扩展,我知道我应该去那里进行调试,而不要使用console.log,但是我最终会在学习中很快学习到。在mapStateToProps和render()console.log中,当我在输入元素中键入内容时,它们什么也没有返回。
我错过了什么导致该应用程序没有更改状态?一些解释将是很好的,这样我就可以了解更多关于redux的信息。谢谢。
答案 0 :(得分:0)
在SignIn.js中
render() {
const { email, onEmailFilled } = this.props;
console.log(`Hello ${email}`);
return{
<form ......
<InputComponent
id_name="signin_email" //this is the name attribute
input_label="Email"
input_type="email"
emailFilled={onEmailFilled}
/>
.......
在InputComponent.js中
<input
id={this.props.id_name}
name={this.props.id_name}
className="input-reset ba b--black-20 pa2 mb2 db w-100 lh-copy lh-copy-l lh-copy-m lh-copy-ns"
type={this.props.input_type}
autoComplete="on"
onChange={this.props.emailFilled}
我通过进行一些较小的更改来解决了这个问题。 Redux扩展也显示更新。解决了。