URL是动态的
url/:id/:name
(使用Reach Router)
url/1/name1
url/2/differnet-link
url/3/another-link
在文章组件中,如果URL是
url/1/name1
,我想要加载1.json
,或者如果URL ID是2,则加载2.json
。import menuArray from '../data/menu.json';
这是我通常在ReactJS中加载JSON的方式
class Article extends React.Component {
constructor(props) {
super(props);
if(/* id is available in the menuArray */) {
// I want to load the JSON by id and store it in state
}
}
}
在ReactJS中加载动态JSON文件的最佳解决方案是什么?