如何解决Reactjs-发送派遣到Reducer的问题吗?
我正在尝试将一个带有路由的网页放在一起,但在使Redux正常工作时遇到了麻烦。我的目标是向减速器发送GET响应,但现在只是测试设置,我的目标是发送true
。我可以从redux存储中检索数据,但似乎无法发送数据,而且不确定可能出问题的地方。这是应该发生的事情:
- 在登录或注册时检查身份验证
- App.js包装在提供程序中
- 用户可以转到Cart.js并通过单击按钮调度值
true
- 可以导航到Menu.js,并且应该能够
console.log
还原器中的新值 - 我的问题:我似乎无法实际分发
true
值。一切都没有中断,但是当我进入“菜单”页面时,控制台日志显示了reducer的初始状态。 - 这在React Native之前对我有用。我想知道是否应该对此进行其他设置?还是身份验证搞砸了?
下面是我的代码示例:
App.js
import React,{ Component } from 'react';
import {
Route,browserRouter as Router,Switch,Redirect,} from "react-router-dom";
import Home from './pages/Home';
import Signup from './pages/Signup';
import Login from './pages/Login';
import Menus from './pages/Menus';
import Carts from './pages/Carts';
import Orders from './pages/Orders';
import Land from './pages/Land';
import { auth } from './services/firebase';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import ourReducer from './store/reducer';
const store = createStore(ourReducer);
global.api = 'https://pitapal.metis-data.site'
//global.api = 'http://localhost:3008';
function PrivateRoute({ component: Component,authenticated,...rest }) {
return (
<Route
{...rest}
render={(props) => authenticated === true
? <Component {...props} />
: <Redirect to={{ pathname: '/login',state: { from: props.location } }} />}
/>
)
}
function PublicRoute({ component: Component,...rest }) {
return (
<Route
{...rest}
render={(props) => authenticated === false
? <Component {...props} />
: <Redirect to='/home' />}
/>
)
}
class App extends Component {
constructor() {
super();
this.state = {
authenticated: false,loading: true,};
}
componentDidMount() {
auth().onAuthStateChanged((user) => {
if (user) {
this.setState({
authenticated: true,loading: false,});
} else {
this.setState({
authenticated: false,});
}
})
}
render() {
return this.state.loading === true ? <h2>Loading...</h2> : (
<Provider store={ store }>
<Router>
<Switch>
<Route exact path="/" component={Signup}></Route>
<PrivateRoute path="/home" authenticated={this.state.authenticated} component={Home}></PrivateRoute>
<PrivateRoute path="/menus" authenticated={this.state.authenticated} component={Menus}></PrivateRoute>
<PrivateRoute path="/carts" authenticated={this.state.authenticated} component={Carts}></PrivateRoute>
<PrivateRoute path="/order" authenticated={this.state.authenticated} component={Orders}></PrivateRoute>
<PublicRoute path="/signup" authenticated={this.state.authenticated} component={Signup}></PublicRoute>
<PublicRoute path="/login" authenticated={this.state.authenticated} component={Login}></PublicRoute>
</Switch>
</Router>
</Provider>
);
}
}
export default App;
Reducer.js
import { combineReducers } from 'redux';
const INITIAL_STATE = {
carts: 'nothing'
};
const ourReducer = (state = INITIAL_STATE,action) => {
const newState = { ...state };
switch (action.type) {
case "CARTS":
return {
...state,carts: action.value
}
break;
}
return newState;
};
export default combineReducers({
reducer: ourReducer,});
Carts.js
class Carts extends Component {
render() {
return (
<Container>
<Button onClick={()=>this.props.setCart(true)}>sendToRedux</Button>
</Container>
);
}
}
const mapStatetoProps = (state) => {
const { reducer } = state
return { reducer }
};
const mapdispachToProps = dispatch => {
return {
setCart: (y) => dispatch({ type: "CARTS",value: y })
};
}
export default connect(mapStatetoProps,mapdispachToProps
)(Carts);
Menu.js
import React,{ Component } from 'react';
import Header from "../components/Header";
import MenuItem from '../components/MenuItem';
import classes from './menus.module.css'
import { auth,db } from "../services/firebase";
import { Container,Col,Row } from 'react-bootstrap';
import { connect } from 'react-redux';
class Menu extends Component {
render() {
console.log('my carts data:',this.props.reducer.carts);
}
return (
<Container>
welcome to menu
</Container>
);
}
}
const mapStatetoProps = (state) => {
const { reducer } = state
return { reducer }
};
export default connect(mapStatetoProps)(Menu);
编辑:
这是我从Redux Devtools截取的屏幕截图,这意味着派遣任务肯定发送正确了吗?
所以看来问题是,当我使用页眉组件浏览页面时,状态被重新加载,整个应用程序被重新布局。想知道是否有人知道这可能是什么。下面是我的标头组件:
Header.js
import { Link } from 'react-router-dom';
import { auth } from '../services/firebase';
//import "./header.css";
import React,{ Component } from 'react'
import { Navbar,Nav,NavItem,NavDropdown,Form,FormControl,Button } from 'react-bootstrap'
// import {Link} from 'react-router-dom'
//import classes from './navbar.module.css';
import 'bootstrap/dist/css/bootstrap.min.css';
class Header extends Component {
render() {
return (
<div>
{auth().currentUser
?
<Navbar className="fixed-top" collapSEOnSelect expand="lg" style={{ backgroundColor: '#485671' }} variant="dark">
<Navbar.Brand href="#home">PitaPal</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="/login">Home</Nav.Link>
<Nav.Link href="/menus">Manage my Menus</Nav.Link>
</Nav>
<Nav>
<Button onClick={() => auth().signOut()} variant="outline-success">Sign Out</Button>
</Nav>
</Navbar.Collapse>
</Navbar>
:
<Navbar className="fixed-top" collapSEOnSelect expand="lg" style={{ backgroundColor: '#485671' }} variant="dark">
<Navbar.Brand href="#home">PitaPal</Navbar.Brand>
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
<Navbar.Collapse id="responsive-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="/login">Login</Nav.Link>
<Nav.Link href="/signup">Sign Up</Nav.Link>
</Nav>
<Nav>
<Nav.Link href="contact">Contact Us</Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
}
</div>
)
}
}
export default Header;
解决方法
问题
您的Header
使用普通锚标签向您的路线页面发布导航,这也会带来页面加载的副作用。
解决方案
使用react-router-dom
Link
组件进行链接。
要么指定as
的{{1}}属性,要么使用Nav.Link
Link
或
<Nav.Link as={Link} to="/login">Home</Nav.Link>
<Link to="/login">Home</Link>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。