gpt4 book ai didi

javascript - 使用 React Router 4 和 Redux 时,React View 不会渲染

转载 作者:行者123 更新时间:2023-12-03 04:06:13 27 4
gpt4 key购买 nike

我想使用 React、React router 4、Redux 创建简单的示例。但我陷入了渲染过程。路由器用户正在正常渲染,但我的主页 View 却没有。我也没有错误,所以我不知道问题是什么。任何帮助我都会非常感激。

文件:

index.js

import React from "react";
import { render } from "react-dom";
import { BrowserRouter as Router,Route,Link } from 'react-router-dom';
import { createStore,applyMiddleware } from "redux";
import thunk from "redux-thunk";
import rootReducer from "./reducers/rootReducer";
import { Provider } from"react-redux"
import { composeWithDevTools } from "redux-devtools-extension";
import { Home } from "./components/home/Home";
import { User } from "./components/user/User";

const store = createStore(
rootReducer,
composeWithDevTools(
applyMiddleware(thunk)
)
);

class App extends React.Component{
render(){
return(
<Provider store={store}>
<Router>
<div>
<Link to="/">Home</Link>
<Link to="/user">User</Link>
<Route exact path="/" component={Home}/>
<Route exact path="/user" component={User}/>
</div>
</Router>
</Provider>

);
}
}

render(<App/>,window.document.getElementById("app"));

rootReducer.js

import { combineReducers } from "redux";
import userReducer from "./userReducer";

export default combineReducers({
user:userReducer
});

userReducer.js

export default function User(state=[{name:'TMP'}],action={}){
switch (action.type){
default: return state
}
}

Home.js

import React from "react";
import {connect} from"react-redux"
import { withRouter } from 'react-router-dom'


class Home extends React.Component{
render(){
return(
<div>
TADAAAAA HOME
{user}
</div>
);
}
}

function mapStateToProps(state) {
return{
user:state.user
}
}
export default withRouter(connect(mapStateToProps)(Home))

user.js

import React from "react";


export class User extends React.Component{
render(){
return(
<div>
USER
</div>
);
}
}

最佳答案

import { Home } from "./components/home/Home";

应该是:

import Home from "./components/home/Home";

Here您可以了解默认导入/导出与否的区别

关于javascript - 使用 React Router 4 和 Redux 时,React View 不会渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44542507/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com