Redux Action 没有传递给 reducer
Redux Action not getting passed to reducer
我试图让我的文本框在用户在框中键入内容时更新字数。但是 setWordCount 操作没有传递给 reducer。我不知道为什么这不起作用。
在故障排除中,我确认该组件正在按照预期的方式关闭初始字数统计状态。我还确认当用户键入内容时会调用 setWordCount。这应该触发将更新的字数传递给状态的操作,但它没有触发。我在控制台中没有收到任何错误,我的中间件记录器 none 正在触发。
这是我的组件。
import React from 'react';
import { connect } from 'react-redux';
import { setWordCount } from '../../redux/writing/writing.actions';
import { UpdateWordCount, UpdatePercentage } from '../../redux/writing/writing.utils';
import './writing-text-box.styles.scss';
const WritingTextBox = ({wordCount, goal}) => {
var updatedPercentage = UpdatePercentage(wordCount, goal);
var percent = updatedPercentage + '%';
return (
<div className='writing-box-container'>
<textarea className='writing-box' type='text' onChange={
(e) => {
setWordCount(UpdateWordCount(e.target.value));
}
}
/>
<div id='Progress'>
<div id='Bar' style={{width: percent}}></div>
</div>
<p key='writing-box-word-count' className='wordcount' >
{wordCount} / {goal}</p>
</div>
)}
const mapStateToProps = state => ({
wordCount: state.writing.wordCount,
goal: state.writing.goal,
percentage: state.writing.percentage
});
const mapDispatchToProps = dispatch => ({
setWordCount: ({wordCount}) => dispatch(setWordCount(wordCount)),
// setPercentage: percentage => dispatch(setPercentage(percentage)),
});
export default connect(mapStateToProps, mapDispatchToProps)(WritingTextBox);
这是我的操作文件,几乎是从另一个可用的应用程序复制粘贴过来的:
import WritingTypes from "./writing.types";
export const setWordCount = wordCount => ({
type: WritingTypes.SET_WORD_COUNT,
payload: wordCount,
});
和我的减速器:
import WritingTypes from "./writing.types";
const INITIAL_STATE = {
wordCount: 0,
goal: 124,
percentage: 0
}
const writingReducer = (currentState = INITIAL_STATE, action) => {
switch(action.type) {
case WritingTypes.SET_WORD_COUNT:
return {
...currentState,
wordCount: action.payload
};
default:
return currentState;
}
}
export default writingReducer;
和我的根 Reducer:
import { combineReducers } from "redux";
import writingReducer from "./writing/writing.reducer";
const rootReducer = combineReducers ({
writing: writingReducer
})
export default rootReducer;
您需要更加小心命名。目前,setWordCount
是以下人的名字:
动作创建者,它只是创建动作对象。
export const setWordCount = wordCount => ({
type: WritingTypes.SET_WORD_COUNT,
payload: wordCount,
});
调度动作的道具。
setWordCount: ({wordCount}) => dispatch(setWordCount(wordCount)),
这里应该是第二个:
<textarea className='writing-box' type='text' onChange={
(e) => {
setWordCount(UpdateWordCount(e.target.value));
}
}
/>
为了让它工作,从 props 中解构它:
const WritingTextBox = ({wordCount, goal,setWordCount}) => {
现在它指向道具,并按预期工作。
我试图让我的文本框在用户在框中键入内容时更新字数。但是 setWordCount 操作没有传递给 reducer。我不知道为什么这不起作用。
在故障排除中,我确认该组件正在按照预期的方式关闭初始字数统计状态。我还确认当用户键入内容时会调用 setWordCount。这应该触发将更新的字数传递给状态的操作,但它没有触发。我在控制台中没有收到任何错误,我的中间件记录器 none 正在触发。
这是我的组件。
import React from 'react';
import { connect } from 'react-redux';
import { setWordCount } from '../../redux/writing/writing.actions';
import { UpdateWordCount, UpdatePercentage } from '../../redux/writing/writing.utils';
import './writing-text-box.styles.scss';
const WritingTextBox = ({wordCount, goal}) => {
var updatedPercentage = UpdatePercentage(wordCount, goal);
var percent = updatedPercentage + '%';
return (
<div className='writing-box-container'>
<textarea className='writing-box' type='text' onChange={
(e) => {
setWordCount(UpdateWordCount(e.target.value));
}
}
/>
<div id='Progress'>
<div id='Bar' style={{width: percent}}></div>
</div>
<p key='writing-box-word-count' className='wordcount' >
{wordCount} / {goal}</p>
</div>
)}
const mapStateToProps = state => ({
wordCount: state.writing.wordCount,
goal: state.writing.goal,
percentage: state.writing.percentage
});
const mapDispatchToProps = dispatch => ({
setWordCount: ({wordCount}) => dispatch(setWordCount(wordCount)),
// setPercentage: percentage => dispatch(setPercentage(percentage)),
});
export default connect(mapStateToProps, mapDispatchToProps)(WritingTextBox);
这是我的操作文件,几乎是从另一个可用的应用程序复制粘贴过来的:
import WritingTypes from "./writing.types";
export const setWordCount = wordCount => ({
type: WritingTypes.SET_WORD_COUNT,
payload: wordCount,
});
和我的减速器:
import WritingTypes from "./writing.types";
const INITIAL_STATE = {
wordCount: 0,
goal: 124,
percentage: 0
}
const writingReducer = (currentState = INITIAL_STATE, action) => {
switch(action.type) {
case WritingTypes.SET_WORD_COUNT:
return {
...currentState,
wordCount: action.payload
};
default:
return currentState;
}
}
export default writingReducer;
和我的根 Reducer:
import { combineReducers } from "redux";
import writingReducer from "./writing/writing.reducer";
const rootReducer = combineReducers ({
writing: writingReducer
})
export default rootReducer;
您需要更加小心命名。目前,setWordCount
是以下人的名字:
动作创建者,它只是创建动作对象。
export const setWordCount = wordCount => ({ type: WritingTypes.SET_WORD_COUNT, payload: wordCount, });
调度动作的道具。
setWordCount: ({wordCount}) => dispatch(setWordCount(wordCount)),
这里应该是第二个:
<textarea className='writing-box' type='text' onChange={
(e) => {
setWordCount(UpdateWordCount(e.target.value));
}
}
/>
为了让它工作,从 props 中解构它:
const WritingTextBox = ({wordCount, goal,setWordCount}) => {
现在它指向道具,并按预期工作。