尽管已初始化

时间:2017-02-24 07:18:43

标签: javascript reactjs redux

我试图从reducer(下面的footerreducer.js)中的mapStatetoProps(下面的FooterLink.js)中返回一个未定义的状态。

我现在只在mapStatetoProp func中定义。

如何返回"返回A"从减速机?

FooterFirst.js是一个连接reducer的组件。

(这也有问题,但我不认为它会导致未定义的状态。也许......)

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
  }
}

0 个答案:

没有答案