简体   繁体   中英

Redux state is not changing according to the input

I started making a react app after learning a bit from react redux tutorial with a register, a login, and a mainpage. I am using redux to handle states and the state is not updated when I typed into the input field in the login page.

In constants.js

export const USER_LOGGED_IN = 'USER_LOGGED_IN';

In actions.js

import * as constants from './constants';

export const userLoggedIn = (text) => ({
  type: constants.USER_LOGGED_IN,
  payload: text
});

In 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;
  }
};

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

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

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

I have redux chrome extension and I know I should be look there to debug instead of using console.log but I will eventually learn in soon as I progress. In the mapStateToProps and in render() console.log, they returned nothing as I typed in input element.

What did I miss that causes this app is not changing the state? Some explanation will be good so that I can understand react redux more. Thank you.

In 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} 
        />
    .......

In 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}

I managed to solve it by making some minor changes. Redux extension is showing the updates as well. Solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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