【问题标题】:Redux. Pass state value to reducer还原。将状态值传递给减速器
【发布时间】:2018-07-09 02:39:49
【问题描述】:

我正在尝试在我的应用中实现 Redux。所以,我创建了动作、reducer、store 等......现在我必须将状态传递给 reducer 并更改此参数的值(布尔值)。我不知道我做错了什么。单击按钮后会触发减速器中的alert,但dialog 不会关闭。知道如何更改open 的值吗?

动作

export const checkPassword = () => ({
    type: "CHECK_PASSWORD"
  });

组件

const mapDispatchToProps = dispatch => {
    return {
        checkPassword: () => dispatch({type: 'CHECK_PASSWORD'})
 };}

function mapStateToProps(state, open) {
   return {
       open: state.open,
   };}

class StartDialog extends Component {
  constructor(props) {
    super(props);
    this.state = { open: true };
  }

render() {
const actions = [ <FlatButton label="Submit" onClick={this.props.checkPassword} /> ];

return (
  <Dialog title="Welcome to the React App!" actions={actions} open={this.state.open} ></Dialog>
);}}

const StartForm = connect(mapStateToProps, mapDispatchToProps)(StartDialog);

export default StartForm;

减速器

import { CHECK_PASSWORD } from "../constants/action-types";

const initialState = {
  open: true
};

const checkpasswordReducer = (state = initialState, action) => {
  switch (action.type) {
    case CHECK_PASSWORD:
    alert('action!')
    return {...state, open: false};
  default:
    return state;
}};

export default checkpasswordReducer;

商店

import { createStore } from "redux";
import rootReducer from "../reducers/index";

const store = createStore(
  rootReducer,
  window.__REDUX_DEVTOOLS_EXTENSION__ && 
window.__REDUX_DEVTOOLS_EXTENSION__()
);

export default store;

reducer index.js

import { combineReducers } from "redux";
import checkpasswordReducer from "./checkpasswordReducer";

export default combineReducers({ checkPassword: checkpasswordReducer 
});

【问题讨论】:

    标签: javascript reactjs redux react-redux


    【解决方案1】:

    open 将在 props 中,而不是在 state

    把你的渲染改成这个

    <Dialog title="Welcome to the React App!" actions={actions} open={this.props.open} ></Dialog>
    

    同样在mapStateToProps 函数中open 值将在状态对象中,因此您不需要函数中的第二个参数

    function mapStateToProps(state) {
       return {
           open: state.checkPassword.open,
       };
    }
    

    【讨论】:

    • 现在,我有一个错误:失败的道具类型:道具openDialogInline中被标记为必填,但它的值是undefined
    • 你能发布如何创建商店吗?
    • 对不起,还有一个人也发布了这个文件../reducers/index
    • @Italik 签出您需要更改的编辑mapStateToProps 函数
    • 好的,差不多完成了。剩下的一件事是,dialog 应该在开始时打开,因为我传递给 initial state true,但 dialog 它已经关闭。当我在这行return {...state, open: false}; 中将值更改为true 时,它可以正常工作。为什么?
    【解决方案2】:

    当您使用redux 并从store 读取值时,您需要在组件中从props 使用它。简而言之,您不应该有一个可以直接从props 派生的state。将您的组件更改为下面,它应该可以工作

    class StartDialog extends Component {
    
        render() {
           const actions = [ <FlatButton label="Submit" onClick={this.props.checkPassword} /> ];
    
           return (
              <Dialog title="Welcome to the React App!" actions={actions} open={this.props.open} ></Dialog>
           );
        }
    }
    

    同样在你的 mapStateToProps 中你需要正确访问 state,如果你使用 combineReducers,你需要从相应的 reducer 访问 open 值

    所以如果你使用combineReducer 喜欢

    const reducers = combineReducer({
       checkPassword:checkpasswordReducer
    })
    

    你需要使用你的mapStateToProps函数

    function mapStateToProps(state) {
       return {
           open: state.checkPassword.open,
       };
    }
    

    【讨论】:

    • 现在,我有一个错误:失败的道具类型:道具openDialogInline中被标记为必需,但它的值是undefined
    • DialogInline 组件在哪里
    • 你在使用 combineReducers 吗??
    • 哦,我的意思是Dialog。所以,我需要在Dialog 组件中为open 添加初始值。但这很奇怪,因为我在 reducer 中写了const initilState
    • 你使用 combineReducer 吗??
    猜你喜欢
    • 2017-12-21
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多