简体   繁体   English

如何将值从函数传递到另一个文件中的另一个组件

[英]How can I pass the value from my function to another component in another file

I'm making a react application that shows details about the pokemon you searched. 我正在制作一个React应用程序,其中显示有关您搜索的神奇宝贝的详细信息。 So I have a Home component which has an input field + submit button. 所以我有一个Home组件,它具有一个输入字段+提交按钮。 I want to render my api call in my Main component. 我想在我的Main组件中呈现我的api调用。

The question that I have is : How can I pass the value from this input field that is located in my home component to my main component when I enter submit? 我的问题是:当我输入Submit时,如何将值从位于home组件中的此输入字段传递给我的主要组件?

home component details 家庭组件详细信息

I need this value to update my pokemon name state in my Main component in order to get the pokemon name for my fetch call. 我需要此值来更新我的Main组件中的神奇宝贝名称状态,以便获取提取调用的神奇宝贝名称。

I need this value to be stored inside my 'searchValue' state. 我需要将此值存储在“ searchValue”状态中。 Main component details 主要组件细节

any tips? 有小费吗?

One of the ways you can achieve this by Using ref in reactjs , Inside your Main Component you need to make a reference, like : 您可以通过在reactjs中使用ref来实现此目的的方法之一,您需要在Main Component内进行引用,例如:

componentDidMount() {
    this.props.onRef(this);
}

// Delete the reference once component is unmounted

componentWillUnmount() {
    this.props.onRef(undefined);
}

And then create a method , which will receive the values from a the Home component and then setState like : 然后创建一个方法,该方法将从Home组件接收值,然后从setState

method(values) {
    this.setState({ searchValue: values });
}

Now inside your Home component you need to reference method component before your input like , (You can amend it accordignly) 现在,在您的Home组件内部,您需要在输入类似之前引用方法组件(您可以对其进行适当修改)

import Home from './Home'

<Home onRef={ref => (this.home = ref)} />
<Form onSubmit={e => { this.onSubmit(e) }}

Make sure to add onSubmit method inside main component which will send the values to Home Component 确保在主要组件内部添加onSubmit方法,该方法会将值发送到Home Component

onSubmit = values => {
    this.home.method(values);
}

You can read more about Ref and the DOM on React Documentation 您可以在React Documentation上阅读有关Ref和DOM的更多信息。

I would propose you to take a look at redux which is great tool for state management. 我建议您看一下redux ,它是状态管理的绝佳工具。 Redux will ensure you have a single source of truth. Redux将确保您拥有单一的事实来源。

What you will have to do is create an Action which would dispatch an event to update the state, a Reducer which would update the state (value of the search-key in redux-store ). 您将要做的是创建一个Action ,该Action会调度一个事件来更新状态,一个Reducer会更新状态( redux-store search-key的值)。 Your Main component will read the data from the API (Same Action - Reducer way) & render it without having to bother about the current state. 您的Main组件将从API(相同的Action - Reducer方式)中读取数据并进行渲染,而无需理会当前状态。 I can explain the whole architecture in detail if you are more interested. 如果您更感兴趣,我可以详细解释整个体系结构。 This architecture will have less bugs & clear flow of information including shared state between components. 这种体系结构将减少错误,减少信息流,包括组件之间的共享状态。

I know this sounds bit too complex but its worth a try if your application is growing. 我知道这听起来有点复杂,但是如果您的应用程序正在增长,则值得尝试。

Few suggestions: Your Main component has too much of logic. 几点建议:您的Main组件逻辑太多。 Components should have least logic as possible so they are more deterministic. 组件应具有尽可能少的逻辑,以便更具确定性。 Please also read about React Stateless functional components 还请阅读有关React无状态功能组件的信息

Here are some links if you wish to take redux path 如果您想使用redux路径,请点击以下链接

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

相关问题 我如何将值从组件传递到ReactJS中的另一个组件 - How can i pass a value from a component to another component in ReactJS 如何将值从一个组件传递到另一个组件? - How can I pass values from one component to another component? 如何将 useState 值从一个组件传递到另一个组件 - How can I pass useState value from one component to another component 如何从另一个组件调用并将值传递给function - How to call and pass the value to function from another component 当我在函数中传递值时,如何将javascript上的值传递给另一个javascript文件? - How to pass value from on javascript to another javascript file when I pass value in function? 我可以从标题中触发另一个组件上的功能吗? - Can i trigger function on another component from my header? 如何在EmberJS中将属性从一个组件传递到另一个组件? - How can I pass an attribute from one component to another in EmberJS? 如何使用 React 将数据从列表传递到另一个组件? - How can I pass a data to another component from a list with React? React / Redux:如何将此状态从一个组件传递到另一个文件? - React/Redux: How can I pass this state from one component to another file? React - 如何将 API 数据从一个组件传递到另一个 js 文件中的另一个组件? - React - How can I pass API data from one component to another in a different js file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM