Javascript 雷杜。将状态值传递给减速器

Javascript 雷杜。将状态值传递给减速器,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我正在尝试对我的应用程序实施Redux。所以,我创造了动作,减速机,存储等等。。。现在我必须将状态传递给reducer并更改这个参数的值(布尔值)。我不知道我做错了什么。单击按钮后会触发减速器中的警报,但对话框不会关闭。知道如何更改打开的值吗 行动 export const checkPassword = () => ({ type: "CHECK_PASSWORD" }); 组成部分 const mapDispatchToProps = dispatch => {

我正在尝试对我的应用程序实施Redux。所以,我创造了动作,减速机,存储等等。。。现在我必须将状态传递给reducer并更改这个参数的值(布尔值)。我不知道我做错了什么。单击按钮后会触发减速器中的
警报
,但
对话框
不会关闭。知道如何更改
打开的值吗

行动

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;
贮藏

减速器索引.js

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

export default combineReducers({ checkPassword: checkpasswordReducer 
});

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

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>
       );
    }
}
您需要使用
MapStateTrops
函数,如

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

打开
将处于
道具
状态

将渲染更改为此

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

现在,我有一个错误:失败的道具类型:道具
打开
对话框内联
中标记为所需,但其值为
未定义
您可以发布如何创建存储吗?抱歉,但是还有一篇文章也将此文件发布到
。/reducers/index
@Italik签出您需要更改的编辑
mapstatetops
功能好的,差不多完成了。剩下的一件事是,
dialog
应该在启动时打开,因为我传递到了
initial state
true,但是
dialog
它关闭了。当我将这一行中的值更改为
true
返回{…状态,open:false}它工作正常。为什么?现在,我有一个错误:失败的道具类型:道具
open
DialogInline
中被标记为必需的,但是它的值是
undefined
哪里是
DialogInline
组件你在使用combinereducer吗?哦,我的意思是
Dialog
。因此,我需要在
对话框
组件中向
打开
添加初始值。但这很奇怪,因为我在reducer中编写了
const initilState
。您使用combineReducer吗??
const reducers = combineReducer({
   checkPassword:checkpasswordReducer
})
function mapStateToProps(state) {
   return {
       open: state.checkPassword.open,
   };
}
<Dialog title="Welcome to the React App!" actions={actions} open={this.props.open} ></Dialog>
function mapStateToProps(state) {
   return {
       open: state.checkPassword.open,
   };
}