【问题标题】:text is used as a state variable but not declaredtext 用作状态变量但未声明
【发布时间】:2021-01-25 22:09:54
【问题描述】:

下面的例子来自docs on useReducer

状态变量text用在

onChangeText={(text) => {
      dispatch({ type: 'first', value: text })
    }}

但据我所知,它并没有事先在任何地方声明。我错过了什么吗?

import React, { useReducer } from 'react'
import { View, Text, TextInput } from 'react-native'

function reducer(state, action) {
  switch (action.type) {
    case 'first':
      return { ...state, first: action.value }
    case 'last':
      return { ...state, last: action.value }
  }
}

export default function App() {
  const [state, dispatch] = useReducer(reducer, { first: '', last: '' })

  return (
    <View>
      <TextInput
        style={{ fontSize: 32 }}
        placeholder="First"
        value={state.first}
        onChangeText={(text) => {
          dispatch({ type: 'first', value: text })
        }}
      />
      <TextInput
        style={{ fontSize: 32 }}
        placeholder="Last"
        value={state.last}
        onChangeText={(text) => {
          dispatch({ type: 'last', value: text })
        }}
      />
      <Text style={{ fontSize: 32 }}>
        Hello {state.first} {state.last}
      </Text>
    </View>
  )
}

如果这是微不足道的事情,我很抱歉让您担心。我是这个环境的新手..!!这个社区很棒。

【问题讨论】:

    标签: reactjs react-native use-reducer


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多