我正在使用 react 16.8 我使用鉤子和功能組件制作了一個項目,現在我試圖將它變成基于類的組件,但沒有安裝一個組件。在 app.js 中,我在 componentDidUpdate 中獲取數據,它工作正常,沒有問題。Element.js 還渲染組件并創建鏈接 onclick 我正在更新狀態并通過傳遞不起作用的道具來調用彈出組件。應用程序.jsimport 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。當彈出窗口在路由之外被調用時,它正在工作。但是在每個組件上點擊我必須傳遞不同的道具并顯示相同的彈出窗口。 元素.jsimport 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 }); };
無法在基于反應類的組件中安裝組件
慕容森
2021-11-12 16:40:41