I am a newbie to react. I have a navigation bar like the one below
import React, { Component, PropTypes } from 'react';
import { Navbar, Nav, NavItem } from 'react-bootstrap';
import { LinkContainer } from 'react-router-bootstrap';
class MainNav extends Component {
constructor(props) {
super(props);
this.state = {
loggedIn: 1,
};
}
render() {
return (
<div>
<Navbar inverse collapseOnSelect>
<Navbar.Header>
<Navbar.Brand>
<a href="/">SportsSpot</a>
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<Nav>
<LinkContainer to="/nfl">
<NavItem>NFL</NavItem>
</LinkContainer>
<LinkContainer to="/mlb">
<NavItem>MLB</NavItem>
</LinkContainer>
<LinkContainer to="/nba">
<NavItem>NBA</NavItem>
</LinkContainer>
<LinkContainer to="/nhl">
<NavItem>NHL</NavItem>
</LinkContainer>
</Nav>
<Nav pullRight>
<LinkContainer to="/login">
<NavItem>Login</NavItem>
</LinkContainer>
<LinkContainer to="/signup">
<NavItem>Sign Up</NavItem>
</LinkContainer>
</Nav>
</Navbar.Collapse>
</Navbar>
<div className="content">
{this.props.children}
</div>
</div>
)
}
}
MainNav.propTypes = {
children: PropTypes.element.isRequired,
};
export default MainNav;
I am trying to dispatch action to the server whenever user clicks on any of the links in the navigation bar so as to fetch the corresponding data. My action creator file looks like the following
export function getCurrentAllNews() {
return {
meta: { remote: true },
type: 'GET_CURRENT_ALL_NEWS'
};
}
export function getCurrentNBANews() {
return {
meta: { remote: true },
type: 'GET_CURRENT_NBA_NEWS'
};
}
export function getCurrentNHLNews() {
return {
meta: { remote: true },
type: 'GET_CURRENT_NHL_NEWS'
};
}
export function getCurrentNFLNews() {
console.log("it comes here:");
return {
meta: { remote: true },
type: 'GET_CURRENT_NFL_NEWS'
};
}
export function getCurrentMLBNews() {
return {
meta: { remote: true },
type: 'GET_CURRENT_MLB_NEWS'
};
}
export function setConnectionState(state, connected) {
return {
type: 'SET_CONNECTION_STATE',
state,
connected
};
}
So for example when the user clicks on the link to NFL i want to add something like
<LinkContainer to="/nfl">
<NavItem onClick={this.props.getCurrentNFLNews} eventKey={1}>NFL</NavItem>
</LinkContainer>
So that I will be able to dispatch the corresponding action for nfl and fetch the required data from the server
I read that connect from 'react-redux' can be used to connect the callback function with action creators. for that i have to use the code like
export const MainNavContainer = connect(mapNavToProps,actionCreators)(MainNav);
But the problem is connect is not working for a navigation bar which already has some routes defined under it. I.e I have a route file like below
import MainNav from './components/navbar.jsx';
const routes =<MainNav>
<App>
<Switch>
<Route exact path="/" component={HomePageContainer}/>
<Route exact path="/nfl" component={NFLHomePageContainer}/>
<Route path ="/login" component = {LoginPage}/>
<Route path ="/signup" component = {SignUpPage}/>
</Switch>
</App>
</MainNav>;
So, trying to add connect to the navbar.jsx throws the following error
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.
My index.jsx has the provider code like below
ReactDOM.render((
<MuiThemeProvider muiTheme={getMuiTheme()}>
<Provider store={store}>
<Router>{routes}</Router>
</Provider>
</MuiThemeProvider>),
document.getElementById('root')
);
I am unaware of how to solve this issue. I would also like to know how to redirect only after receiving the data from the server so that wrong info is not displayed in a new page.Thanks for the help in advance