最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use react.js with a multi page website (Non SPA)? - Stack Overflow

programmeradmin0浏览0评论

I have a laravel app. For pages with such routing: /admin/entity/ i want to use react ponents with react router to handle /admin/entity/:id route.

If i use browserify to bundle all ponents in one file, i can't access any ponent to render it from outside since browserify wraps it to closure. Thus, i have few questions:

  • Should i create separate bundle.js file for each page and render ponents explicitly in that file?
  • Or should i prepile each ponent from jsx to js and render it inline from *.blade.php file?
  • Does it make sense to include react library to bundle.js or i can load it explicitly from view?

    I have a laravel app. For pages with such routing: /admin/entity/ i want to use react ponents with react router to handle /admin/entity/:id route.

    If i use browserify to bundle all ponents in one file, i can't access any ponent to render it from outside since browserify wraps it to closure. Thus, i have few questions:

  • Should i create separate bundle.js file for each page and render ponents explicitly in that file?
  • Or should i prepile each ponent from jsx to js and render it inline from *.blade.php file?
  • Does it make sense to include react library to bundle.js or i can load it explicitly from view?

    Share Improve this question edited Jun 30, 2015 at 12:38 Callum Linington 14.4k14 gold badges80 silver badges156 bronze badges asked Jun 30, 2015 at 10:21 Semyon ZhikharevSemyon Zhikharev 6645 silver badges10 bronze badges 2
    • 2 If your bundle has a client-side router in it then it doesn't matter if it's a single bundle (for that reason) – nanobar Commented Jun 30, 2015 at 10:23
    • but if i user client-side router it captures the whole url on every page and tells that route is not found in other pages – Semyon Zhikharev Commented Jun 30, 2015 at 10:47
    Add a ment  | 

    1 Answer 1

    Reset to default 4

    Here are my opinions:

    • You should bundle React and all your code in all pages that will have any part rendered by React ponents (From what I gather, /admin/entity).
    • Your React ponent should always be rendered to a specific element (e.g., an empty <div>) and should have something like a React Router configured so that it ignores /admin/entity but renders /admin/entity/:id. Your challenge is that any links that point to a different :id URL should optimally be inside the React ponents, using Link ponents. This will hook up your router automatically.

    Your routes will probably look like this:

    <Route path="admin/entity/:id" handler={Entity} />
    

    I believe that, when you run the Router, if the browser's URL isn't in the expected format, React won't actually render anything.

  • 发布评论

    评论列表(0)

    1. 暂无评论