简体   繁体   中英

React Native (getting value from text input)

So I'm doing a little personal project and I'm having some trouble getting the numeric value from the textinput. I'm either getting an error saying the function expected a string or undefined.

Below is the source code:

//Timer state
  const [timer, setTimer] = useState({
    work: 0.3,
    break: 0.2,
    longBreak: 0.5,
    active: "work",
  });

  const workTime = timer.work;
  const breakTime = timer.break;
  const longBreakTime = timer.longBreak;

This is where the value is supposed to be passed :

function handleChange(name, value) {
    console.log(value);

    switch (name) {
      case "work":
        setTimer({ ...timer, work: parseInt(value) });
        break;
      case "break":
        setTimer({ ...timer, break: parseInt(value) });
        break;
      case "longBreak":
        setTimer({ ...timer, longBreak: parseInt(value) });
      default:
        break;
    }
  }

I'm trying to retrieve the value from this text input:

 <TextInput
              style={styles.input}
              onChange={(workTime) => handleChange("work", setTimer(workTime))}
              placeholder="Work"
              keyboardType="numeric"
              numeric
              value={workTime}
              name="work"
            />
onChange={(workTime) => handleChange("work", workTime)}

您正在传递对钩子函数的调用,而不是从输入中读取的值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM