Create a subdomain like slack does e.g. : https://someteam.slack.com/ in REACT JS

橙三吉。 提交于 2020-06-16 17:31:25

问题


I am trying to implement feature like slack does in react. You all know before logged-in on Slack, the url looks like this https://www.slack.com/ , but once you logged in it change to https://www.team.slack.com/.

So i have my react project with 2 different layouts (landing layout & client dashboard). What i am looking for is..

Landing layout should runs on https://www.example.com/ and once client logged in successfully the domain get change into https://www.clientname.example.com/ and admin layout gets render.

Need helps how to implement this dynamic subdomain based rendering react-components.


回答1:


First of all, redirect all requests to index.html. After that, you can use window.location.host. Just parse this parameter and render your component regarding to parsed data.

const parsedData = window.location.host.split(".");

if(parsedData.length >= 3){
    const subDomain = parsedData[0];
    ReactDOM.render(<SubDomainApp subDomain={subDomain} />, document.getElementById('root'));
}else{
    ReactDOM.render(<MainApp />, document.getElementById('root'));
}


来源:https://stackoverflow.com/questions/52143565/create-a-subdomain-like-slack-does-e-g-https-someteam-slack-com-in-react-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!