简体   繁体   中英

Can I use react or CSS to target a specific component to change onclick?

I'm trying to have a navbar that when a link on it is clicked, it renders the new component into the component B box in the picture and nowhere else on the screen (aka everything else stays the same). Do I need react router for this or can I do this using straight up React and CSS?

Aka I click link 1, or link 2 or link 3 and the result shows up in Component box B.

在此处输入图片说明

Do I need react router for this or can I do this using straight up React and CSS?

You could do it w/o react-router by making some statefull component that uses links to change its path and selecting component to render based on this path . In the end react-router is using React.

handleLinkClick(e, path) {
  ev.preventDefault()
  this.setState({path})
}

renderBComponent() {
   const { path } = this.state;

   const ComponentToRender = {path1: ComponentB1, path2: ComponentB2, etc}
   return <ComponentToRender />
}

But you'd better use react-router :) to avoid reinventing the wheel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM