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