React Presentation组件无法使用React容器从Redux存储中读取<input />的值

时间:2019-04-20 14:48:44

标签: javascript reactjs typescript redux react-redux

我是stackoverflow的新手,也是使用react / redux的新手。我已经浏览了很多帖子,看看是否有类似的帖子可以为我提供答案,但我仍然感到困惑。

我目前有一个表示性组件“重复”和一个容器组件,用于从redux存储中获取道具并将动作从表示性组件分发到redux存储中。当我在输入字段中输入数据时,我有演示组件更新redux存储,但是我想使用redux存储来检索输入值,以便当用户首次进入页面时,输入值为“ 0”这是redux存储内的初始值。

我最初使用react / redux制作了一个简单的Counter组件,并且工作正常。此后,我制作了“重复”组件,并更改了Redux存储,以使用CombinedReducer,这是问题开始出现的时候,因为两个组件都无法从Redux存储中读取内容。

Rootreducer.ts

import { combineReducers } from "redux";
import countReducer from "./example/reducer";
import repetitionsReducer from "./reps/reducer";

const rootReducer = combineReducers({
    countReducer,
    repetitionsReducer
})

export default rootReducer;

RepetitionsReducer.ts

import { RepetitionsState } from "../types";
import { AddRepetitionsAction } from "./actions";

export type RepetitionsActionType = AddRepetitionsAction;

export type Dispatch = (action: RepetitionsActionType) => void;

// The reducer updates the count
const initialState: RepetitionsState = {
  repetitions: 0
};

const repetitionsReducer = (
  state = initialState,
  action: RepetitionsActionType
): RepetitionsState => {
  switch (action.type) {
    case "ADD_REPETITIONS":
      return { ...state, repetitions: action.repetitions };
    default:
      return state;
  }
}

export default repetitionsReducer;

RepetitionsContainer.ts

import { connect } from "react-redux";
import { RootState } from "../../store/types";
import { Dispatch } from "../../store/reps/reducer";
import { addRepetitions } from "../../store/reps/actions";
import Repetitions from "../../components/reps/Repetitions";

interface StateFromProps {
  repetitions: number ;
}

interface DispatchFromProps {
  updateRepetitions: (repetitions: number) => void;
}

export type RepetitionsProps = StateFromProps & DispatchFromProps;

const mapStateToProps = (state: RootState): StateFromProps => ({
  repetitions: state.repetitions
});

const mapDispatchToProps = (dispatch: Dispatch): DispatchFromProps => ({
  updateRepetitions: (repetitions: number) => dispatch(addRepetitions(repetitions))
});

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Repetitions);

RepetitionsComponent.ts
注意:,当我尝试console.log“重复”时,我现在不确定。

import React from "react";
import { RepetitionsProps } from "../../containers/reps/Repetitions";

const Repetitions: React.FunctionComponent<RepetitionsProps> = ({ 
  repetitions, 
  updateRepetitions
}) => {
  console.log(repetitions)
  return (
    <div>
      <h3>Reps</h3>
      <input
        onChange={(event) => updateRepetitions(Number(event.target.value))}
        value={ repetitions } // <-- This is the value i'm wanting to present to the user from the redux store
      />                         
    </div>
  );
};

export default Repetitions;

App.ts

import React from "react";
import ReactDOM from "react-dom";
import * as serviceWorker from "./serviceWorker";
import Header from "./components/header/Header";
import { Provider } from "react-redux";
import { createStore } from "redux";
import Counter from "./containers/example/Counter";
import Repetitions from "./containers/reps/Repetitions";
import { composeWithDevTools } from 'redux-devtools-extension';
import rootReducer from "./store/reducer";

const store = createStore(rootReducer, composeWithDevTools());

console.log(store.getState())

function App() {
  return (
    <div className="App">
      <Header title={"Rep count"} />
      <Repetitions />
      <br />
      <br />
      <br />
      <Counter />
    </div>
  );
}


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

当用户第一次加载页面时,我希望看到的预期结果是在“ Reps”标题下方的输入框中显示的“ 0”。取而代之的是,该框为空,但是redux存储将重复的值显示为“ 0”。

reps-input-desired-results

还值得注意的是,当我第一次加载页面时,输入字段下方的计数器用于从redux存储读取“ 0”,但是现在它也未定义。

谢谢您抽出宝贵的时间查看我的帖子。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

嗯...这里有些问题:

首先,您的重复状态当前正在保存一个对象。它应该包含一个简单的数字。

第二,商店中重复状态的名称(来自您附加的快照)是“ repetitionReducer”,而不是“ repetition”,因为您尝试在mapStateToProps中获取它:

const mapStateToProps = (state: RootState): StateFromProps => ({
  repetitions: state.repetitions // <- this one here...
});

希望这会有所帮助:)