使用 React Router 渲染组件时 Redux 崩溃
2019-01-02
13235
我有一个 React 应用,它开始变得无法控制,所以我决定使用 React-Redux。一切都进行得很顺利,直到我遇到了一个奇怪的错误。它显示:
TypeError: this is undefined
如果 React-Redux 库中没有抛出这个错误,那它就没什么了不起的了。像这样:
PureComponent
node_modules/react/cjs/react.development.js:412
409 | */
410 |
411 | function PureComponent(props, context, updater) {
> 412 | this.props = props;
413 | ^ this.context = context; // If a component has string refs, we will assign a different object later.
414 |
415 | this.refs = emptyObject;
控制台显示:
The above error occurred in one of your React components:
in Unknown (created by Context.Consumer)
in Connect(Component) (at RegisterFormUI.jsx:32)
in form (at RegisterFormUI.jsx:21)
in div (at RegisterFormUI.jsx:18)
in div (at RegisterFormUI.jsx:15)
in div (at RegisterFormUI.jsx:14)
in div (at RegisterFormUI.jsx:13)
in Unknown (created by Context.Consumer)
in Connect(Component) (created by Route)
in Route (at SharedPaths.jsx:18)
in Switch (at NavigationUI.jsx:98)
in Router (created by BrowserRouter)
in BrowserRouter (at NavigationUI.jsx:65)
in Unknown (created by Context.Consumer)
in Connect(Component) (at App.js:50)
in div (at App.js:49)
in App (at src/index.js:15)
in Provider (at src/index.js:14)
这没有任何意义,因为我的
RegisterFormUI
组件中没有调用
Connect
方法。我不知道我搞砸了什么。这是我的表单 UI:
export default ({user = {}}) => {
return <BrowserRouter>
<React.Fragment>
<Navbar className='navbar_all'>
<Navbar.Header>
<Navbar.Brand>
<LinkContainer className='navbar_brand' id='home' to='/'>
<NavItem>
<img alt='ZdajTo' src="assets/images/new_logo.png" style={{height: '30px'}}/>
</NavItem>
</LinkContainer>
</Navbar.Brand>
</Navbar.Header>
<Nav className='float_right'>
<LinkContainer to='/homepage' style={{textDecoration: 'none'}}>
<NavItem>
<button style={{
backgroundColor: '#F16049',
border: '4px solid #F16049',
borderRadius: '4px',
padding: '10px',
marginBottom: '5px',
color: '#fff'
}}>
DLA ROZWIĄZUJĄCYCH
</button>
</NavItem>
</LinkContainer>
</Nav>
<Nav className='float_right'>
<SharedNavBar user={user.userAcc}/>
{renderNavBar(user)}
</Nav>
</Navbar>
<Switch>
{renderPaths(user) ? renderPaths(user).map(path => path) : null}
{SharedPaths.map(path => path)}
</Switch>
</React.Fragment>
</BrowserRouter>
};
我的容器是这样的:
import {connect} from 'react-redux';
import UI from '../../ui/studentRegistrationUI/RegisterFormUI'
import {beginRegistration} from "../../../../actions";
const mapStateToProps = state => ({user: state.user});
const mapDispatchToProps = dispatch => ({
handleClick() {
dispatch(
beginRegistration('student')
)
}
});
export default connect(mapStateToProps, mapDispatchToProps)(UI)
知道我搞砸了什么吗?
1个回答
我的一个子组件出现了问题。我主要尝试这样做:
{CheckBox('Regulations', '/assets/regulamin.pdf', 'regulamin')}
我应该这样做:
<CheckBox id={'Regulations'} path={'/assets/regulamin.pdf'} text={'regulamin'}/>
这是供将来参考的容器:
import {connect} from 'react-redux';
import UI from '../../../ui/studentRegistrationUI/components/CheckBoxUI'
const mapStateToProps = (state, props) => ({user: state.user, id: props.id, path: props.path, text: props.text});
export default connect(mapStateToProps)(UI)
Alex Ironside
2019-01-03