简体   繁体   English

从响应中获取价值并转移到 React 中的另一个组件

[英]Get value from response and transfer to another component in React

I have this handleSubmit that returns me a key (verifyCode) that I should use in another component.我有这个 handleSubmit,它返回一个我应该在另一个组件中使用的密钥(verifyCode)。 How can I pass this verifyCode to another component?如何将此verifyCode传递给另一个组件?

const SendForm = ({ someValues }) => {

      const handleSubmitAccount = () => {
        dispatch(createAccount(id, username))
        
          .then((response) => {
            // I get this value from data.response, its works
            const { verifyCode } = response;
          })
          .catch(() => {
          });
      };

      return(
         //the form with handleSubmitAccount()
       )
}

export default SendForm;

The other component is not a child component, it is loaded after this submit step.另一个组件不是子组件,它是在此提交步骤之后加载的。 But I don't know how to transfer the const verifyCode .但我不知道如何传输const verifyCode

This is the view where the components are loaded, it's a step view, one is loaded after the other, I need to get the const verifyCode in FormConfirmation这是加载组件的视图,它是一个步骤视图,一个接一个加载,我需要在FormConfirmation中获取const verifyCode

<SendForm onSubmit={handleStepSubmit} onFieldSubmit={handleFieldSubmit} />
<FormConfirmation onSubmit={handleStepSubmit} onFieldSubmit={handleFieldSubmit} />

Does anyone know how I can do this?有谁知道我怎么能做到这一点?

You need to move up the state to a component that has both as children and then pass down a function that updates as a prop您需要将状态向上移动到具有两个子项的组件,然后传递一个更新为 prop 的函数

import React from "react";

export default function App() {
    const [value, setValue] = React.useState(0);
        return (
            <div className="App">
                <Updater onClick={() => setValue(value + 1)} />
                <ValueDisplay number={value} />
           </div>
       );
}

 const Updater = (props) => <div onClick={props.onClick}>Update State</div>;
 const ValueDisplay = (props) => <div>{props.number}</div>;

Check out the docs here此处查看文档

For more complex component structures or where your passing down many levels you may want to look into reactContext对于更复杂的组件结构或向下传递多个级别的地方,您可能需要查看reactContext

import React from "react";

//Set Default Context
const valueContext = React.createContext({ value: 0, setValue: undefined });

export default function App() {
    const [value, setValue] = React.useState(0);
        return (
           <div className="App">
              {/** Pass in state and setter as value */}
              <valueContext.Provider value={{ value: value, setValue }}>
                  <Updater />
                  <ValueDisplay />
              </valueContext.Provider>
         </div>
    );
}

const Updater = () => {
     /** Access context with hook */
     const context = React.useContext(valueContext);
     return (
         <div onClick={() => context.setValue(context.value + 1)}>Update State</div>
     );
};
 const ValueDisplay = () => {
     /** Access context with hook */
     const context = React.useContext(valueContext);
      return <div>{context?.value}</div>;
 };

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

相关问题 做出反应。 如何从另一个组件获取单选按钮值 - React. How to get radio button value from another component 如何在反应中将数据从一个组件传输到另一个组件? - How to transfer data from one component to another in react? Angular 2将ajax调用响应转移到另一个组件 - Angular 2 transfer ajax call response to another component React:如何从一个组件获取输入值以在另一个组件中进行Ajax调用? - React : how to get input value from one component to make the ajax call in another component? React 值从一个组件传递到另一个组件 - React value passing from one component to another 将值从一个组件传递到另一个React - passing the value from one component to another React 从 Functional React Component 中的 JSON Response 中获取一个特定值并将其设置为 Select 选项 - Get one specific value from JSON Response in Functional React Component and set it as a Select option 如何使用路由器/路由器参数将数据从一个父组件传输到另一个父组件 - How to transfer data from one parent component to another parent component in react using router / router params 从另一个组件获取参数 [React JS] - Get arguments from another component [React JS] React-从另一个组件获取Div元素 - React - Get Div Element from another Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM