简体   繁体   English

通过输入传递道具 - React

[英]Passing Props With Input - React

Within a child component I have state to store input.在子组件中,我有 state 来存储输入。

My goal is to pass the input state to the parent component.我的目标是将输入 state 传递给父组件。

    //Child component 

    const [userInput, setUserInput] = useState("");

    <TextField value={input} onInput={e => setInput(e.target.value)} />

I tried creating a function to be called on input within the parent function but I cannot seem to get the value "input" passed.我尝试创建一个 function 以在父级 function 中的输入上调用,但我似乎无法获得传递的值“输入”。 What is the correct way to do this?这样做的正确方法是什么? Thanks!谢谢!

You can move the state to the parent component.您可以将 state 移动到父组件。 Then pass the props from the child to the parent.然后将道具从孩子传递给父母。

function Child({
    ...rest
}) {
    return <TextField {...rest} />
}
function Parent() {
    const [input, setInput] = useState(''); // '' is the initial state value
    return (
        <Child  value={value} onInput={e => setInput(e.target.value)} />
    )
}

you were approaching correctly.你走对了。 You can pass a function to get the data from the child to the parent.您可以传递 function 以从子级获取数据到父级。 Check this example:检查这个例子:

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [text, setText] = useState("");

  const getInput = (t) => {
    console.log(t);
    setText(t);
  };

  return (
    <div className="App">
      <Component getInput={getInput} value={text} />
      <p>{text}</p>
    </div>
  );
}

const Component = ({ getInput, value }) => {
  return (
    <>
      <h1> Test </h1>
      <input type="text" value={value} onChange={(e) => getInput(e.target.value)}></input>
    </>
  );
};

The parent App is retrieving the text from the child Component passing the function getInput .App正在通过 function getInput从子Component检索文本。 You only have to decide with event you want to detonate.您只需要决定要引爆的事件。 Of course, this is with input not with TextField.当然,这是针对input而不是 TextField。 I think that you are using TextField from React Material UI, if that's the case you may check the docs to know how to trigger an event.我认为您正在使用 React Material UI 中的 TextField,如果是这种情况,您可以查看文档以了解如何触发事件。

I finally figured this out.我终于想通了。

// This is in the parent component 

const [userInput, setUserInput] = useState("");

// Step 1: Create a function that handles setting the state 
const inputHandler = (userInput) => {
setUserInput(userInput);
}

<PhaseTwoTemplate onInput={inputHandler}  />
//Step 2: Add a function that calls the handler

... ...

// This is the child component PhaseTwoTemplete.js

const [input, setInput] = useState(""); 
// This second state holds the input data for use on child component
// It also is an easy way to hold the data for the parent function. 

 
<TextField 
onChange={e => setInput(e.target.value)}
value={input}  
onInput={onInputHandler} 
/>

// Step 3: Pass the user input to the parent
const onInputHandler = () => {
props.onInput(input);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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