gpt4 book ai didi

reactjs - react 路由器 - 通过 url 参数传递 Prop

转载 作者:行者123 更新时间:2023-12-03 19:39:12 28 4
gpt4 key购买 nike

我正在开发一个使用 react-router 的多语言和多币种应用程序, react-router-redux & react-router-bootstrap用于路由。

目前用户使用默认语言和货币到达应用程序,但是我希望能够在 url 中提供一些参数来设置语言或货币 - 类似于 http://myapp.com/page1?lang='fr'
我还希望能够在任何页面上使用这些参数,以便:
http://myapp.com/page1?lang='fr'http://myapp.com/page2/example1/test?lang='ru'两者的处理方式相同。基本上我希望路由器检查传递给它的任何 url 中是否存在这些参数,如果它们存在则执行一个函数。

我在 react-router 的文档中找不到任何明确的信息。但我相信这是可能的。谁能指出我正确的方向?

最佳答案

您可以通过以下方式访问查询参数this.props.location.query.langs 也提到的组件内部@卢卡斯 .

我提供了一个工作示例以便更好地理解。

除此之外,在您的情况下,我建议您查看 onEnter react 路由器中可用的钩子(Hook)。

let Router = ReactRouter.Router;
let RouterContext = Router.RouterContext;
let Route = ReactRouter.Route;
let Link = ReactRouter.Link;

function redirectBasedOnLang(nextState, replace) {
// check nextState.location.query
// and perform redirection or any thing else via
console.log("called");
console.log(nextState.location.query);
}

const Dashboard = (props) => {
return (
<b className="tag">{"Dashboard"}</b>
);
}

FrDashboard

const FrDashboard = (props) => {
return (
<div>
<h3> Dashboard </h3>
<hr/>
Primary language: {props.location.query.lang}
</div>
);
}

class App extends React.Component {

render() {
return (
<div>
<h2>App</h2>

{/* add some links */}
<ul>
<li><Link to={{ pathname: '/dashboard', query: { lang: 'fr' } }} activeClassName="active">Dashboard</Link></li>
</ul>
<div>
{this.props.children}
</div>
</div>
)
}
};

App.contextTypes = {
router: React.PropTypes.func.isRequired,
};

ReactDOM.render(<Router history={ReactRouter.hashHistory}>
<Route path="/" component={App}>
<Route path="dashboard" component={Dashboard} onEnter={redirectBasedOnLang} />
<Route path="dashboardfr" component={FrDashboard} />
</Route>
</Router>, document.getElementById('test'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/3.0.0/ReactRouter.min.js"></script>


<div id="test"></div>



let Router = ReactRouter.Router;
let RouterContext = Router.RouterContext;
let Route = ReactRouter.Route;

const Link = (props) => {
return (
<b className="lang" onClick={() => props.onClick(props.lang)}>{props.lang}</b>
);
}

class App extends React.Component {

constructor(props) {
super(props);

this.handleLang = this.handleLang.bind(this);
this.state = { langs: [] };
}

handleLang(lang) {
let langs = [].concat(this.state.langs);
if(langs.indexOf(lang) != -1) {
langs = langs.filter(item => item != lang);
}
else langs.push(lang);

this.setState({langs: langs});

this.context.router.push({
pathname: '/',
query: { langs: langs }
});
}

render() {

return (
<div>
<Link onClick={this.handleLang} lang={"fr "} />
<Link onClick={this.handleLang} lang={"en "} />
<Link onClick={this.handleLang} lang={"hi "} />

<div>
<hr/>
<br/>
active languague :
<div>{this.props.location.query.langs ? [].concat(this.props.location.query.langs).join(',') : ''}</div>
</div>
</div>
)
}
};

App.contextTypes = {
router: React.PropTypes.func.isRequired,
};

ReactDOM.render(<Router history={ReactRouter.hashHistory}>
<Route path="/" component={App} />
</Router>, document.getElementById('test'));
.lang {
display: inline-block;
background: #d3d3d3;
padding: 10px;
margin: 0 3px;
cursor: pointer;
width : 50px;
border : 1px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/3.0.0/ReactRouter.min.js"></script>

<div id="test"></div>

关于reactjs - react 路由器 - 通过 url 参数传递 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42393967/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com