我试图从reducer(footerreducer.js下面)返回mapStatetoProps(FooterLink.js下面)中的一个未定义的状态。mapStateToProps总是返回未定义的Redux,尽管初始化
现在我只能在mapStatetoProp func中获取undefined。
如何从reducer返回“返回的A”?
FooterFirst.js是连接减速器的组件。
(有什么不对这个为好,但我不猜它会导致不确定的状态。也许......)
index.js是actioncreator。
FooterLink.js
import {connect} from "react-redux"
import {changeWindow} from "../actions"
import {undefinedText} from "../actions"
import FooterFirst from "../components/FooterFirst"
const mapStateToProps =(state,ownProps)=>{
return {
text:state.text
}
}
const mapDispatchToProps = (dispatch,ownProps)=>{
if (ownProps.text === `undefined`){
return{
onClick:()=>{dispatch(undefinedText(ownProps.text))}
}
}
return{
onClick:()=>{dispatch(changeWindow(ownProps.text))}
}
}
const FooterLink = connect(
mapStateToProps,
mapDispatchToProps
)(FooterFirst)
export default FooterLink
footerreducer.js
const footerreducer = (state,action) =>{
if (typeof state=== 'undefined') {
return Object.assign({},state,{
text:"A"
})
}
switch (action.type){
case `A`:
return Object.assign({}, state, {
text:`returned A`
})
case `B`:
return {
text:`retruned B`
}
case `C`:
return {
text:`returned C`
}
default:
return state
}
}
export default footerreducer
FooterFirst.js
import React ,{PropTypes} from "react"
let x = 0
const FooterFirst = ({text,onClick})=>{
if(!text){
x = x + 1
console.log(x)
return <a text={text} onClick={e=>{e.preventDefault()
onClick()
}}>
AAAA{text}
</a>
}
return (
<a text={text} onClick={e=>{e.preventDefault()
onClick()
}}>
iefefer
</a>
)
}
FooterFirst.PropTypes ={
text:PropTypes.string.isRequired,
onClick:PropTypes.func.isRequired
}
export default FooterFirst
index.js
export const changeWindow = (text_Now)=>{
return {
type:`A`,
text:text_Now
}
}
export const undefinedText = (text_Now)=>{
return {
type:`Notext`,
text:text_Now
}
}
你能在这里重现这个:http://www.webpackbin.com/ –