我是React的新手。我部分遵循本教程full-stack-redux-tutorial來創建我的Web應用程序。我正在使用'react-redux'將我從服務器收到的數據傳遞給我的某個組件。我index.jsx文件看起來像下面在React中使用Provider時無法獲取props.children
import routes from './routes.jsx';
const socket = io(`${location.protocol}//${location.hostname}:8090`);
socket.on('curr_all_news', curr_all_news =>
store.dispatch(setCurrentAllNews(curr_all_news))
);
const createStoreWithMiddleware = applyMiddleware(
remoteActionMiddleware(socket)
)(createStore);
const store = createStoreWithMiddleware(reducer);
ReactDOM.render((
<MuiThemeProvider muiTheme={getMuiTheme()}>
<Provider store={store}>
<Router>{routes}</Router>
</Provider>
</MuiThemeProvider>),
document.getElementById('root')
);
的routes.jsx文件看起來像下面
import MainNav from './components/navbar.jsx';
import App from './components/App.jsx';
import {HomePageContainer} from './components/SportsHome.jsx';
const routes =<MainNav component = {App}>
<Switch>
<Route exact path="/" component={HomePageContainer}/>
<Route path ="/login" component = {LoginPage}/>
<Route path ="/signup" component = {SignUpPage}/>
</Switch>
</MainNav>;
我App.jsx具有下面的代碼
import { Component } from 'react';
export default class App extends Component {
render() {
return this.props.children;
}
};
最後,我首頁有以下代碼
export class HomePage extends PureComponent{
render() {
return <div className="news-holder">
<div className="containerWrapper">
<div className="s-row">
<div className="col-sm-12">
Checking the workng of custom written div
<NewsPanel {...this.props} />
</div>
</div>
</div>
</div>;
}
};
function mapNewsToProps(curr_all_news){
console.log("curr_all_news here:",curr_all_news);
return {
news:curr_all_news
}
}
export const HomePageContainer = connect(mapNewsToProps,actionCreators)(HomePage);
我的問題是我無法在我的主頁上獲得curr_all_news的價值.i.e我只是在mapNewsToProp函數中獲得一個空列表(List {size: 0, _origin: 0, _capacity: 0, _level: 5, _root: undefined…})
作爲curr_all_news的值,而不是被認爲是對象列表的值。我能夠從服務器獲取值,並通過套接字將其傳遞給客戶端,但在App.jsx中,這是給予MainNav的組件,渲染功能永遠不會被調用。我無法弄清楚我缺少的東西提前.Thanks的幫助
我navbar.jsx看起來像下面
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;
的問題是,應用程序組件不指定路線的孩子。爲了確保我希望看到navbar.jsx –
的代碼,我也包含了我的navbar.jsx代碼。 – RBN
這看起來不對我'render(){return this.props。兒童; } :(:( – btzr