单击文本框时变量未定义

时间:2016-06-13 11:12:52

标签: javascript reactjs scope

一旦我点击输入文本框(通过点击任何单选按钮设置值后),我不明白为什么我的容器会丢失变量search_type的值?这是范围问题吗?

const DisplayTweets = ({dispatch, state}) => {
  let self = this
  let input
  let search_type

  let searchTypeSet = (radioValue) => {
    console.log(radioValue)
    switch(radioValue) {
      case 'username':
        search_type = radioValue
        break
      case 'hashtag':
        search_type = radioValue
        console.log(search_type)
        break
      case 'search':
        search_type = radioValue
        console.log(search_type)
        break
      default:
        search_type = 'search'
        break
    }
  }

  return (
    <div>
      <form>
        <input type="text" ref={node => {
          input = node
        }} placeholder="search for tweets" />

        <input type="radio" name='search_type' value='username' onClick={searchTypeSet.bind(self, 'username')} />
          By username
        <input type="radio" name='search_type' value='hashtag' onClick={searchTypeSet.bind(this, 'hashtag')} />
          By hashtag
        <input type="radio" name='search_type' value='search' onClick={searchTypeSet.bind(DisplayTweets, 'search')} />
          By search query

        <button onClick={e => {
          e.preventDefault()
          console.log(input.value)
          console.log(search_type)
          dispatch(fetchTweetsIfNeeded(input.value, search_type))
        }}>
          Get Tweets
        </button>
      </form>

      <ShowTweetComponent state={state} />

    </div>
  )
}

0 个答案:

没有答案