简体   繁体   English

根据道具更改react组件的样式

[英]Change the style of the react component based on its props

I have a Header.js : 我有一个Header.js

const Header = () => {
 return (
  <header className="header">
    <Logo/>
    <HeaderMenu target="home-link"/>
  </header>);
}

and a HeaderMenu.js : HeaderMenu.js

class HeaderMenu extends React.Component {
 render() {
    return(
        <nav className="header-menu">
            <a id="home-link">Home</a>
            <a id="project-link">Projects</a>
            <a id="blog-link">Blog</a>
            <a id="about-me-link">About Me</a>
        </nav>
    );
 }
}

How can I change an a elements style based on target props? 我怎样才能改变a基于元素的风格target的道具?
Example: if target="home-link" , then an a element with id="home-link" has its text underlined and another a element doesn't. 例如:如果target="home-link" ,然后一个a与元素id="home-link"有其文字下划线,并且另一a元素没有。

You can do this by applying a class to any element that you want to have styled differently. 您可以通过将一个类应用于您希望样式不同的任何元素来实现。 In your example of giving a different style to the link for the current page, you can iterate over your links and give an "active" class to the link whose id matches the target. 在为当前页面的链接赋予不同样式的示例中,您可以遍历链接,并为ID与目标匹配的链接赋予“活动”类。

For Example: 例如:

class HeaderMenu extends React.Component {
 render() {
    const { target } = this.props;

    const linkData = [
        {id: "home-link", title: "Home"},
        {id: "project-link", title: "Projects"},
        {id: "blog-link", title: "Blog"},
        {id: "about-me-link", title: "About Me"},
    ];
    const linkElements = linkNames.map(e => (
        <a id={ e.id } className={ e.id === target ? "active" : "" }>{ e.title }</a>
    ));

    return(
        <nav className="header-menu">
            { linkElements }
        </nav>
    );
 }
}

However if you use React Router (which you may want to do so the page doesn't refresh when a link is clicked) the <NavLink> component takes an activeClassName prop, which applies the given className any time the current location (url) matches the NavLink's to prop (which is analogous to the a tag's href ). 但是,如果您使用React Router (您可能希望这样做,则单击链接时页面不会刷新), <NavLink>组件采用activeClassName ,该activeClassName将在当前位置(URL)匹配时应用给定的className。该NavLink的to支撑(这是类似于a标记的href )。

  1. Save menu data into an variable . 将菜单数据保存到variable
  2. Use map to add a tag 使用map添加a标签
  3. Set an className = 'active' on the a where target is matched. 在匹配目标的a上设置className = 'active'
  4. then write css to add underline. 然后编写CSS以添加下划线。

HeaderMenu.js

class HeaderMenu extends React.Component {

 constructor(props) {
     super(props);
     this.menus = [
       { id: 'home-link', name: 'Home' },
       { id: 'project-link', name: 'Projests' },
       // ...
     ];
}

 render() {
    return(
        <nav className="header-menu">
            {
              this.menus.map(menu => (<a id={menu.id} className={this.props.target === menu.id ? 'active' : ''}>{menu.name}</a>))
            }
        </nav>
    );
 }

} }

css : css

.active {
   // text style when active
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 ID 设置 React 组件的样式 - Style a React component based on its ID 根据 Child props 更改父组件 -&gt; Preact/ React - Change Parent component based on Child props -> Preact/ React React 组件不会在 props 更改时重新渲染 - React component not rerendering on props change 反应:在功能组件中更改 State 也更改功能组件的道具值,及其父 Class State - React: Changing State in functional component also Change functional component's props value, and its Parent Class State 在另一个组件道具更改上反应组件重新安装 - React component remounting on another component props change 根据样式组件中的父道具更改嵌套组件的样式? - change nested component's style based on parent props in styled-components? 根据 React Hooks 中父组件的不同变化来改变子组件的不同 props? - Change different props of child component based on different changes in parent component in React Hooks? 为什么这个React / Mobx观察者组件在其可观察道具发生变化时不会重新渲染? - Why does this React/Mobx observer component not rerender when its observable props change? 如何重用React组件而无需更改其内部道具? - How do I reuse a React Component without having to change its inner props? 如果React组件的属性更新但不改变其价值,它会重新渲染吗? - Will a React component re-render if its props are updated, but don't change in value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM