【发布时间】:2020-12-23 15:41:07
【问题描述】:
我在 Login.js 中有以下代码 -
import React from 'react'
import {Button} from "reactstrap";
import 'bootstrap/dist/css/bootstrap.min.css';
import { VerifyCredentials } from "./LoginReducers/Login_Action";
import {GlobalProvider,GlobalConsumer} from "../GlobalStore";
import { LoginReducer } from "./LoginReducers/Login_Reducers";
function Login() {
return (
<div>
<GlobalConsumer>
{
store=>{
store.dispatch(LoginReducer)
}
}
</GlobalConsumer>
<form>
<input type="text" name="txtLoginId" ></input>
<input type="password" name="txtPassword" ></input>
<Button color="success"></Button>
</form>
</div>
)
}
export default Login
我有 Login_Reducer.js-
import Axios from "axios";
import { VerifyCredentials } from "./Login_Action";
const initialState={
userName:"",
password:"",
isVarified:false
}
const url='http://localhost:52016/api/values/';
export const LoginReducer=(state=initialState,action)=>{
switch (action.type) {
case 'VERIFY_CREDENTIALS':
Axios.get(url)
.then(x=>{
alert(x.data);
})
default:
break;
}
}
如何在按钮点击时调用 store.dispatch?
我试过了 -
<Button color="success" onClick={() => store.dispatch({ type: 'VERIFY_CREDENTIALS' })}></Button>
但这并没有帮助
【问题讨论】:
-
首先像这样改变你的减速器:stackoverflow.com/a/65420360/12608714,然后把你的表单放在
GlobalConsumer中。 -
你应该使用 react-redux 包react-redux.js.org/introduction/quick-start
标签: javascript reactjs redux