简体   繁体   English

将数据从组件传递到另一个反应

[英]Pass data from on component to another react

I have a questionnaire and send an request to get all data back from the database, that match the answers.我有一份问卷并发送请求以从数据库中取回与答案匹配的所有数据。 I then want to use the data in another component to display the results.然后我想使用另一个组件中的数据来显示结果。

 <div className='row justify-content-end mt-3 pb-5'>
   <div className='btn btn-primary btn-block rounded shadow-sm result' onClick=this.getSelectedOptions}>Ergebnisanalyse</div>
</div>

My onClick triggers the function in a class component called "ImpactAnalysis" where i get my results back as an array of objects.我的 onClick 触发了一个名为“ImpactAnalysis”的类组件中的函数,在其中我将结果作为对象数组返回。 Then i want to display the result in a class component "ResultPage"然后我想在类组件“ResultPage”中显示结果

You can pass like this:你可以这样通过:

<ResultPage data={impactAnalysisResults} />

Then you will have this array on your ResultPage component.然后,您将在 ResultPage 组件上拥有此数组。

const ResultPage = ({data}) => {
    const { impactAnalysisResults } = data;
}

Simply send those data in a component as props.只需将这些数据作为道具发送到组件中。 to send props to a component add key name and pair value with it.向组件发送道具添加键名和配对值。 As example例如

<ResultPage data={impactAnalysisResults} />

from ResultPage catch values like从 ResultPage 捕获值,例如

function ResultPage ({data}) {
    console.log(data) //check data object sent from parent component
    return (
    //show your data from 'data' props
    <div>{data}</div>
   )
}

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

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