React-Redux 连接不工作
React-Redux connect not working
我正在尝试在我的 codepen 笔中使用 React+Redux,但是连接没有注入 prop,
也许是因为我是业余爱好者,所以我错过了一些东西。请看一看。
http://codepen.io/sahil28v/pen/EKEKME?editors=0010
const { Component } = React;
const { createStore, bindActionCreators, applyMiddleWare, combineReducers } = Redux;
const { Provider, connect } = ReactRedux;
const Application = () => (
<div className="ground">
<Tmap />
</div>
);
class Tmap extends Component {
constructor(props){
super(props);
console.log(this.props.mapstate); // This is returning undefined,no idea why
console.log(store.getState().mapstate); // Though this returns val: "hey" as it should properly though.
}
render () {
return (
<div>
</div>
);
}
}
const mapStateToProps = (state) => ({
mapstate: state.mapstate
});
connect(mapStateToProps)(Tmap);
const initialState = {
val: "hey"
}
const mapReducer = (state = initialState, action) => {
return state ;
} ;
const rootReducer = combineReducers({
mapstate: mapReducer,
});
const store = createStore(rootReducer);
React.render(
<Provider store={store}>
<Application />
</Provider>, document.getElementById('app')
);
此外,我可以在 codepen 本身中在这里配置存储以使用 redux devtools(chrome 扩展)。
我尝试添加 ...window.devToolsExtension ? window.devToolsExtension() : f => f ,到创建商店,但这不起作用。
虽然有人在回答 -- 你也可以推荐好的 documentation/video 用于:
->学习 ES6 ...
-- 感谢帮助
连接 returns 我想是该组件的一个新实例。这有效,使用 connect 作为装饰器。
@connect(
state => ({
mapstate: state.mapstate
})
)
class Tmap extends Component {
constructor(props){
super(props);
alert(this.props.mapstate); // This is returning undefined,no idea why
alert(store.getState().mapstate); //As I understand this is the direct low level api which connect is meant to abstract away along with store.subscribe.
}
render () {
return (
<div>
</div>
);
}
}
您忘记将连接结果保存到新变量中。这个新变量是您应该使用的,而不是 Tmap。
const CTmap = connect(...
....
<CTmap/>
我遇到了同样的问题,我在下面解决了!
首先,你应该像下面这样导出connect
输出对象↓
class Tmap extends Component { ... }
export default connect(mapStateToProps)(Tmap);
然后,然后在routes
中添加导出的对象而不是class,像↓
import TmapConnectOutput from './Components/Tmap';
export const routes = <Layout>
<Route exact path='/Tmap' component={TmapConnectOutput as any} />
</Layout>;
不导出 function
或 class
导出 connector
class
//don't use export here
function ProductCounterPage(props) {
const {
ProductItem,
OrderItemList,
onCountChange,
...rest
} = props;
...............
}
export default connect(mapStateToProps, null)(ProductCounterPage);
我正在尝试在我的 codepen 笔中使用 React+Redux,但是连接没有注入 prop,
也许是因为我是业余爱好者,所以我错过了一些东西。请看一看。 http://codepen.io/sahil28v/pen/EKEKME?editors=0010
const { Component } = React;
const { createStore, bindActionCreators, applyMiddleWare, combineReducers } = Redux;
const { Provider, connect } = ReactRedux;
const Application = () => (
<div className="ground">
<Tmap />
</div>
);
class Tmap extends Component {
constructor(props){
super(props);
console.log(this.props.mapstate); // This is returning undefined,no idea why
console.log(store.getState().mapstate); // Though this returns val: "hey" as it should properly though.
}
render () {
return (
<div>
</div>
);
}
}
const mapStateToProps = (state) => ({
mapstate: state.mapstate
});
connect(mapStateToProps)(Tmap);
const initialState = {
val: "hey"
}
const mapReducer = (state = initialState, action) => {
return state ;
} ;
const rootReducer = combineReducers({
mapstate: mapReducer,
});
const store = createStore(rootReducer);
React.render(
<Provider store={store}>
<Application />
</Provider>, document.getElementById('app')
);
此外,我可以在 codepen 本身中在这里配置存储以使用 redux devtools(chrome 扩展)。 我尝试添加 ...window.devToolsExtension ? window.devToolsExtension() : f => f ,到创建商店,但这不起作用。
虽然有人在回答 -- 你也可以推荐好的 documentation/video 用于: ->学习 ES6 ...
-- 感谢帮助
连接 returns 我想是该组件的一个新实例。这有效,使用 connect 作为装饰器。
@connect(
state => ({
mapstate: state.mapstate
})
)
class Tmap extends Component {
constructor(props){
super(props);
alert(this.props.mapstate); // This is returning undefined,no idea why
alert(store.getState().mapstate); //As I understand this is the direct low level api which connect is meant to abstract away along with store.subscribe.
}
render () {
return (
<div>
</div>
);
}
}
您忘记将连接结果保存到新变量中。这个新变量是您应该使用的,而不是 Tmap。
const CTmap = connect(...
....
<CTmap/>
我遇到了同样的问题,我在下面解决了!
首先,你应该像下面这样导出connect
输出对象↓
class Tmap extends Component { ... }
export default connect(mapStateToProps)(Tmap);
然后,然后在routes
中添加导出的对象而不是class,像↓
import TmapConnectOutput from './Components/Tmap';
export const routes = <Layout>
<Route exact path='/Tmap' component={TmapConnectOutput as any} />
</Layout>;
不导出 function
或 class
导出 connector
class
//don't use export here
function ProductCounterPage(props) {
const {
ProductItem,
OrderItemList,
onCountChange,
...rest
} = props;
...............
}
export default connect(mapStateToProps, null)(ProductCounterPage);