简体   繁体   English

ReactJS在不同的HTML页面上呈现组件

[英]Reactjs render component on different html pages

I am building a new Nodejs app and I would like to use Reactjs for some components. 我正在构建一个新的Nodejs应用程序,我想将Reactjs用于某些组件。 So far I have two html pages; 到目前为止,我有两个html页面; home page and my_orders page. 主页和my_orders页面。 The home page displays all the food items, and the my_orders page presents the food items that the user has ordered. 主页显示所有食品,而my_orders页面显示用户订购的食品。

To do this I build 2 react components, a Meal component and a MealContainer component. 为此,我构建了2个React组件,一个Meal组件和一个MealContainer组件。 I need to put the MealContainer component in both the index.html page and the my_orders.html page. 我需要将MealContainer组件放在index.html页面和my_orders.html页面中。 So far tho I have this: 到目前为止,我有这个:

var MealsContainer = React.createClass({
 //render the right meals
})

ReactDom.render(<MealsContainer url='/getMyMeals' />,document.getElementById('my-meals-box'))
ReactDom.render(<MealsContainer url='/getAllMeals'/>, document.getElementById('meals-box'))

The meals-box is in the index.html page and the my-meals-box is in the my_orders page. 饭盒位于index.html页,而my-meals框位于my_orders页。

This doesn't work because when I am rendering index.html my-meals-box is not a real id and vice versa. 这不起作用,因为在渲染index.html my-meals-box时,它不是真实的ID,反之亦然。 The ideal way would be to be able to call render in the html page and render the elements with the right props. 理想的方法是能够在html页面中调用render并使用正确的props渲染元素。

I could use webpack to create different .js files to import in the different html pages but I really don't like that idea. 我可以使用webpack创建不同的.js文件,以将其导入不同的html页面,但是我真的不喜欢这个想法。 What is the Reactjs best practice for this situation? 在这种情况下,Reactjs最佳实践是什么?

EDIT: 编辑:

I am looking for a standard way partially react apps deal with inserting components in html pages. 我正在寻找一种标准的方法来部分响应应用程序处理在html页面中插入组件的问题。 I am not looking for a quick workaround. 我不是在寻找快速解决方法。

Considering they are different pages, and they will probably have even more differences as your system grows, a more reasonable and sustainable approach would be to split the build in multiple entrypoints, for instance. 考虑到它们是不同的页面,并且随着系统的增长它们可能会有更多差异,例如,一种更合理和可持续的方法是将构建分为多个入口点。

- common.js // Will contain the components - index-page.js // Will render in the my-meals-box element - my-orders-page.js // Will render in the meals-box element.

Then in the index.html you should include the common.js and the index-page.js files. 然后,在index.html ,应包括common.jsindex-page.js文件。 And in the my_orders.html you should include the common.js and the my-orders-page.js files. my_orders.html您应该包含common.jsmy-orders-page.js文件。

You can even use a name convention that every js file that ends with -page is an entry point to avoid having to do that manually every time you add a new page. 您甚至可以使用一个名称约定,即以-page结尾的每个js文件都是一个入口点,以避免每次添加新页面时都必须手动执行此操作。

Or you can use my solution: 或者您可以使用我的解决方案:

if (window.location.pathname === '/') {
    ReactDOM.render(
        <Hello />,
        document.getElementById('react-index')
    );
} else if (window.location.pathname === '/test/') {
    ReactDOM.render(
        <TestVu />,
        document.getElementById('testVu')
    );
}

I don't measure it's performance or scalability yet. 我还没有衡量它的性能或可扩展性。 So I also want to hear your opinion 所以我也想听听你的意见

Wow, this is slightly confusing to me. 哇,这让我有些困惑。 Are you only building half your app in React and if so why? 您仅在React中构建一半的应用程序,如果是,为什么呢?

The way that I am used to thinking of this is: All one single app. 我习惯于思考的方式是:所有一个应用程序。 Or a Single Page App. 或单页应用程序。 One index.html page and the rest of the content is dynamically generated within said page. 一个index.html页面,其余内容在该页面内动态生成。 And then you just need to make sure that the site URL and the content shown are aligned. 然后,您只需要确保网站URL和显示的内容对齐即可。 You would use react router ( https://github.com/ReactTraining/react-router ) for that. 您将为此使用react路由器( https://github.com/ReactTraining/react-router )。

Also, I don't get the code you posted. 另外,我没有收到您发布的代码。 If you could clarify that one, it might be easier to trouble shoot. 如果您可以澄清这一点,则可能更容易排除故障。 You declare a component MealsContainer but you don't actually call it. 您声明了MealsContainer组件,但实际上并未调用它。 Instead you call MealsBox? 相反,您叫MealsBox? Did you export the component like that? 您是否这样导出了组件?

I have never seen the use of a url in the ReactDom.render method other than to pass it down as props. 除了将它作为道具传递外,我从未见过在ReactDom.render方法中使用URL。 I am not sure that this works. 我不确定这是否有效。 Assuming it doesn't then you are calling the same component on different DOM elements that might not be present. 假设没有,那么您将在可能不存在的不同DOM元素上调用相同的组件。

If you don't want to use Router, the way you might be able to go about it is with separate js files that get called when the current .html loads. 如果您不想使用Router,则可能的解决方法是使用单独的js文件,这些文件会在当前.html加载时被调用。

//Render component MealsBox into the existing element with the ID meals-box 
ReactDom.render(<MealsBox url='/getMyMeals' />,document.getElementById('meals-box'))

//Render MealsBox into the existing element with the ID my-meals-box
ReactDom.render(<MealsBox url='/getAllMeals'/>, document.getElementById('my-meals-box'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM