gpt4 book ai didi

javascript - 如何区分哪个组件调用了回调函数?

转载 作者:行者123 更新时间:2023-12-03 02:11:11 24 4
gpt4 key购买 nike

我是新来 react 的,很抱歉,如果这是新手问题。我有一个组件 Dropdown,它通过回调函数返回一个值。我想渲染两次以选择两个不同的值,然后简单地渲染下面选定的值。我怎样才能允许你的两个不同的组件向组件发送不同的数据。下面是我的代码。

index.js

import { Dropdown } from './components/dropdown'

class App extends Component {
constructor(props) {
super(props);
this.calculateRate = this.calculateRate.bind(this);
this.callApi = this.callApi.bind(this);
this.state = {
response: "",
currA: 0,
currB: 1
}
}

componentDidMount() {

this.callApi()
.then(res => this.setState({ response: res.express }))
.catch(err => {console.log(err)});

}

callApi = async () => {
const response = await fetch('/main');
const body = await response.json();
if (response.status !== 200) throw Error(body.message);
return body;
}

calculateRate = (key, val) => {
// if the calling agent sent currA data, update currA,
// else if the calling agent sent currB data, update currB
if (key === 'A') this.setState({currA: val})
if (key === 'B') this.setState({currB: val})
console.log('updated curr' + key + ' to ' + val);
}

render() {
return (
<div className='App'>
<div>
<Dropdown callbackFromParent={this.calculateRate}
stateKey={'A'} val={this.state.currA} />
<Dropdown callbackFromParent={this.calculateRate}
stateKey={'B'} val={this.state.currB} />
</div>
</div>
);
}
}


export default App;

dropdown.js

export class Dropdown extends React.Component {

constructor(props){
super(props);
this.state = {
list: [],
selected: ""
};
}

componentDidMount(){
fetch('https://api.fixer.io/latest')
.then(response => response.json())
.then(myJson => {
this.setState({ list: Object.keys(myJson.rates) });
});
}

render(){
var selectCurr = (curr) =>
<select
onChange={event => props.callbackFromParent(props.stateKey, event.target.value)}
>
{(this.state.list).map(x => <option>{x}</option>)}
</select>;

return (
<div>
{selectCurr()}
</div>
);
}
}

最佳答案

我不太确定您想要实现什么,但希望以下内容展示了如何允许两个不同的组件将不同的数据发送到 <App>组件。

重要的变化是:我们需要将方法绑定(bind)到 <App> constructor() 中的组件函数,那么我们可以使用.bind() Dropdown中的方法组件指定要传递给回调函数的数据:

import React, { Component } from 'react';
import './App.css';

class App extends Component {
constructor(props) {
super(props);
this.calculateRate = this.calculateRate.bind(this);
this.callApi = this.callApi.bind(this);
this.state = {
response: "",
currA: 0,
currB: 1
}
}

componentDidMount() {
/*
this.callApi()
.then(res => this.setState({ response: res.express }))
.catch(err => {console.log(err)});
*/
}

callApi = async () => {
const response = await fetch('/main');
const body = await response.json();
if (response.status !== 200) throw Error(body.message);
return body;
}

calculateRate = (key, val) => {
// if the calling agent sent currA data, update currA,
// else if the calling agent sent currB data, update currB
if (key === 'A') this.setState({currA: val})
if (key === 'B') this.setState({currB: val})
console.log('updated curr' + key + ' to ' + val);
}

render() {
return (
<div className='App'>
<div>
<Dropdown callbackFromParent={this.calculateRate}
stateKey={'A'} val={this.state.currA} />
<Dropdown callbackFromParent={this.calculateRate}
stateKey={'B'} val={this.state.currB} />
</div>
</div>
);
}
}

const Dropdown = props => (
<select onChange={event => props.callbackFromParent(props.stateKey, event.target.value)}>
<option value='cats'>Cats</option>
<option value='dogs'>Dogs</option>
</select>
)

export default App;

关于javascript - 如何区分哪个组件调用了回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49564870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com