React演示组件无法使用反应容器从redux商店读取值

问题描述 投票:0回答:1

我是stackoverflow的新手,并且使用react / redux是一个新手。我已经扫描了很多帖子,看看是否有类似的帖子可以给我一个答案,但我仍然感到困惑。

我目前有一个演示组件“Repetitions”和一个容器组件,用于从redux store获取props,并从presentational组件到redux store调度操作。当我在输入字段中输入数据时,我的表示组件更新了redux存储,但是我想使用redux存储来检索输入值,这样当用户第一次进入页面时输入值为“0”这是redux商店内的初始值。

我最初使用react / redux制作了一个简单的Counter组件,它工作正常。我已经制作了“Repetition”组件并更改了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”,但现在它也是未定义的。

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

javascript reactjs typescript redux react-redux
1个回答
0
投票

嗯......这里有些不对劲:

首先,你的重复状态目前正在持有一个对象。它应该包含一个简单的数字。

其次,当您尝试在mapStateToProps中获取重复状态时,商店中的重复状态的名称(来自您附加的快照)是“repetitionReducer”而不是“重复”:

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

希望这可以帮助 :)

© www.soinside.com 2019 - 2024. All rights reserved.