我正在使用 react 16.8 我使用钩子和功能组件制作了一个项目,现在我试图将它变成基于类的组件,但没有安装一个组件。在 app.js 中,我在 componentDidUpdate 中获取数据,它工作正常,没有问题。Element.js 还渲染组件并创建链接 onclick 我正在更新状态并通过传递不起作用的道具来调用弹出组件。
应用程序.js
import React, { Component } from 'react';
import './App.css';
import axios from 'axios';
import Element from './components/Element';
class App extends Component {
constructor(props) {
super(props);
this.state = { elements: [] };
}
componentDidMount() {
const res = async () => {
const result = await axios.get('/data');
const data = result.data;
this.setState({ elements: data });
};
res();
}
render() {
return (
<div className='wrapper'>
<div id='table'>
{this.state.elements.map(element => (
<Element elements={element} key={element._id} />
))}
</div>
</div>
);
}
}
export default App;
在 Element.js 中,我为所有元素创建链接并创建路由部分。Onclick 使 showpopup 为 true 并将道具传递给 popup。当弹出窗口在路由之外被调用时,它正在工作。但是在每个组件上点击我必须传递不同的道具并显示相同的弹出窗口。 元素.js
import React, { Component } from 'react';
import {
BrowserRouter as Router,
Redirect,
Route,
Link
} from 'react-router-dom';
import Popup from './Popup';
class Element extends Component {
constructor(props) {
super(props);
this.state = { showPopup: false };
}
handleClick = () => {
this.setState({ showPopup: !this.state.showPopup });
};
慕田峪4524236
料青山看我应如是
侃侃无极
相关分类