gpt4 book ai didi

javascript - 你如何在 React 中换出一个组件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:29:46 24 4
gpt4 key购买 nike


我正在尝试使用 React 构建单页应用程序。目前我有一个名为 App 的组件,它由 ReactDOM 呈现,它包含导航,然后是在导航组件之后呈现的组件(或页面)。

import React from 'react';
import Navigation from './Navigation';
import NavigationLink from './NavigationLink';
import Home from './Home';
import About from './About';

const App = () => (
<div>
<Navigation>
<NavigationLink onClick={ ... }>Home</NavigationLink>
<NavigationLink onClick={ ... }>About</NavigationLink>
</Navigation>
<Home />
</div>
);

export default App;

我希望能够选择“关于”链接并将 Home 组件更新为 About。我正在寻找适用于两个以上元素(或页面)的解决方案。

这是我当前有效的代码,但这是一个非常糟糕的解决方案。

import React, { Component } from 'react';
import {
NavItem,
NavLink,
UncontrolledDropdown,
DropdownToggle,
DropdownMenu,
DropdownItem,
} from 'reactstrap';
import firebase from '../../utils/firebase';
import Navigation from '../../components/Navigation/Navigation';
import Login from '../Login/Login';
import Profile from '../Profile/Profile';
import Home from '../Home/Home';

class App extends Component {
state = {
currentShowingPage: 0,
currentlyLoggedInUser: null,
isLoginModalOpen: false,
};

componentDidMount = () => {
firebase.auth().onAuthStateChanged((currentUser) => {
if (currentUser) {
this.setState({
currentlyLoggedInUser: currentUser,
isLoginModalOpen: false,
});
} else {
this.setState({
currentlyLoggedInUser: null,
});
}
});
};

handleLoginModalToggle = () => {
this.setState((previousState) => ({
isLoginModalOpen: !previousState.isLoginModalOpen,
}));
};

render = () => {
let currentShowingPageComponent;

const {
isLoginModalOpen,
currentlyLoggedInUser,
currentShowingPage,
} = this.state;

if (currentShowingPage === 0) {
currentShowingPageComponent = <Home />;
} else if (currentShowingPage === 1) {
currentShowingPageComponent = <Profile />;
}

return (
<div>
<Login
isModalOpen={isLoginModalOpen}
modalToggler={this.handleLoginModalToggle}
/>
<Navigation>
{currentlyLoggedInUser ? (
<UncontrolledDropdown nav>
<DropdownToggle nav caret>
{currentlyLoggedInUser.email}
</DropdownToggle>
<DropdownMenu right>
<DropdownItem
onClick={() =>
this.setState({
currentShowingPage: 1,
})
}
>
Profile
</DropdownItem>
<DropdownItem disabled>Expeditions</DropdownItem>
<DropdownItem divider />
<DropdownItem onClick={() => firebase.auth().signOut()}>
Sign Out
</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
) : (
<NavItem>
<NavLink onClick={this.handleLoginModalToggle}>Login</NavLink>
</NavItem>
)}
</Navigation>
{currentShowingPageComponent}
</div>
);
};
}

export default App;

最佳答案

这只是一个非常简单的切换组件的示例,有很多方法可以做到这一点,我相信您会得到更好的练习答案,但希望这能给您一些想法。 (如果不是很明显,您可以通过调用 setState({currentComponent: 'compX'})) 来使用它

getComponent(){
let component;
switch (this.state.currentComponent){
case 'compA' :
component = <CompA/>;
break;
case 'compB' :
component = <CompB/>;
break;
case 'compC' :
component = <CompC/>;
break;
case 'compD' :
component = <CompD/>;
break;
}
return component;
}

render(){
return(
<div>
{this.getComponent()}
</div>
);
}

关于javascript - 你如何在 React 中换出一个组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51412139/

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