1

I using Next.js

I have this structure:

enter image description here

I need to open the same component for each URLs, like this 'http://localhost:3000/hakkimizda', 'http://localhost:3000/cerez-politikasi', 'http://localhost:3000/kullanim-kosullari' This component:

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

import { withRouter } from 'next/router';

class InfoPage extends Component {
    render() {
        return <div className="info-page">
            <div className="info-page__header">
            </div>
        </div>;
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(withRouter(InfoPage));

How can I open the same component in several different URLs in Next.js?

3
  • You can use rewrites. Commented Dec 29, 2020 at 9:19
  • 1
    Or dynamic routing. Commented Dec 29, 2020 at 9:22
  • or redirects Commented Dec 29, 2020 at 9:44

1 Answer 1

0

You can make your page a component and render that component in all these pages:

  • components/Shared.js:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

import { withRouter } from 'next/router';

class InfoPage extends Component {
    render() {
        return <div className="info-page">
            <div className="info-page__header">
            </div>
        </div>;
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(withRouter(InfoPage));
  • pages/hakkimizda.js
  • pages/cerez-politikasi.js
  • pages/kullanim-kosullari.js
import React from 'react`;
import Shared from '../components/Shared/';

export default function Page() {
  return (
    <Shared />
  );
}
Sign up to request clarification or add additional context in comments.

2 Comments

I know that How can create just one file to URLs ? not 3 (pages/hakkimizda.js, pages/cerez-politikasi.js, pages/kullanim-kosullari.js )
As @98sean98 suggested earlier, you'll want dynamic routing for that.

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.