1

I'm totally new in reactjs world.I have a navigation bar which is placed in 100px from the top. when i scroll down my page that navigation bar should be sticky on top.

my code of navigation bar class is

export default class NavBar extends React.Component {
  constructor(props) {
    super(props);

    this.toggle = this.toggle.bind(this);
    this.state = {
      isOpen: false
    };
  }
  toggle() {
    this.setState({
      isOpen: !this.state.isOpen
    });
  }
  render() {
    return (
      <div id="stickyheader">
        <Navbar color="light" white expand="md">
          <NavbarBrand href="/">
            <ResizeImage
              src={require('../image/logo.png')}
              alt="logo"
              options={{ width: 10 },{ height: 50 },{ mode: 'pad' }}

            />
          </NavbarBrand>
          <NavbarToggler onClick={this.toggle} />
            <Collapse isOpen={this.state.isOpen} navbar>
              <Nav className="ml-auto" navbar>
                <NavItem>
                  <NavLink href="/components/">HOME</NavLink>
                </NavItem>
                <NavItem>
                  <NavLink href="#">ALL CARS</NavLink>
                </NavItem>
                <NavItem>
                  <NavLink href="#">RATES</NavLink>
                </NavItem>
                <NavItem>
                  <NavLink href="#">ABOUT US</NavLink>
                </NavItem>
                <NavItem>
                  <NavLink href="#"><FontAwesomeIcon icon="users" />  BECOME A PARTNER</NavLink>
                </NavItem>

              </Nav>
            </Collapse>
        </Navbar>
      </div>

    );
  }
}

jquery code

$(function(){
        // Check the initial Poistion of the Sticky Header
        var stickyHeaderTop = $('#stickyheader').offset().top;

        $(window).scroll(function(){
                if( $(window).scrollTop() > stickyHeaderTop ) {
                        $('#stickyheader').css({position: 'fixed', top: '0px'});
                        $('#stickyalias').css('display', 'block');
                } else {
                        $('#stickyheader').css({position: 'static', top: '0px'});
                        $('#stickyalias').css('display', 'none');
                }
        });
  });

this jquery code is working fine when I used in html. I guess the problem is to indicate of id="stickyheader" I referred some links but I didn't find proper solution. please guide me to solve this.Thanks for advanced.

4
  • 1
    One way to acheive is using ref but you can use jquery library for it. like npm i jquery and then import $ from 'jquery' Commented Sep 29, 2018 at 17:38
  • @Revansiddh I added use jquery library already. but please give any hint how to use ref insead of id Commented Sep 29, 2018 at 17:42
  • 1
    reactjs.org/docs/refs-and-the-dom.html. Like ref={this.myRef} at div and this.myRef = React.createRef(); in constructor Commented Sep 29, 2018 at 18:05
  • 1
    I think you should consider not using jQuery. jQuery will only add extra data overhead of the downloading size of your bundle file when that code, you present, can easily be done by native javascript. But if you don't want to start from scratch, there are a lot of open sourced packages doing this. ex: npmjs.com/package/react-sticky Commented Sep 30, 2018 at 0:24

1 Answer 1

1

You can init jQuery function after react initial render - just call it from componentDidMount.

Sign up to request clarification or add additional context in comments.

2 Comments

please specify how can I do it sir?
stackoverflow.com/q/33351288/6124657 - you can place your code inside cDM or call fn - from this moment (time) div (with id) is available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.