【发布时间】:2018-12-31 11:28:42
【问题描述】:
这里是代码沙盒:https://codesandbox.io/s/lp005rj59q
我希望它显示 Hello Goodbye ,但它只是显示 Goodbye
我正在为我正在开发的新 React/Redux 应用程序构建框架,<h1>{value}</h1> 返回未定义。我知道这一点,因为我将 console.log(value) 放在了我的类中的渲染和返回方法之间。
我已经 将状态映射到道具:
const mapStateToProps = state => ({
value: state.value
});
创建了我的商店:
import { createStore } from "redux";
import reducers from "./reducers";
const store = createStore(reducers);
export default store;
创建了初始状态为“Hello”的减速器
const initialState = {
value: "Hello"
};
function reducer(state = initialState, action) {
switch (action.type) {
default:
return state;
}
}
export default reducer;
创建了一个连接函数:
export default connect(
mapStateToProps,
mapDispatchToProps
)(Clock);
this.props 的附加值:
export class Clock extends React.Component {
render() {
const { value } = this.props;
console.log(value);
return (
<div>
<h1>{value}</h1>
<h2> Goodbye</h2>
</div>
);
}
}
并将商店传递给提供者元素:
const AppWrapper = () => (
<Provider store={store}>
<Clock />
</Provider>
);
render(<AppWrapper />, document.getElementById("root"));
我错过了什么?
【问题讨论】:
标签: javascript reactjs redux react-redux jsx