react-page-layout | Create layouts for react | Frontend Framework library
kandi X-RAY | react-page-layout Summary
kandi X-RAY | react-page-layout Summary
This library allows for you to concentrate all your layout logic into its own components. It allows you to create a layout that can have serveral where you can inject content. A layout aware component can use the and to fill out the slots.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of react-page-layout
react-page-layout Key Features
react-page-layout Examples and Code Snippets
Community Discussions
Trending Discussions on react-page-layout
QUESTION
In many programming languages - eg. go and c# razor you have the ability to define placeholder / areas (layouts) which you can inject content into from other templates/views.
I know in some frontend frameworks this is also possible eg. aurelia where you define slots. I also found this library https://github.com/dschnare/react-slot and https://github.com/monvillalon/react-page-layout which doesn't seem like it is actively maintained.
Then I found this: https://reactjs.org/docs/composition-vs-inheritance.html which seem to do something similarly except it goes the wrong direction. Here the parent will have to know about the child, since you pass it in from the parent.
Instead I'd like to just define a placeholder in the parent, which I can reference from the child, and somehow insert a grand child into.
Here's an illustration of what I'm trying to do:
Hope it makes sense. How can this be done in React?
...ANSWER
Answered 2018-Jan-22 at 22:52You could use callback props to get this to work, though I would advise against it.
On the parent, define a function called showSettingsIcon
that takes a boolean parameter and pass it to the child as a prop. The child would have logic to determine whether it has a settings child and call the function. Make sure the function is "bound" to the parent, just like you would any other callback prop.
You could have another callback prop, setGrandchildComponent
, or something, that would take a component instance or component type as a parameter. When called, the parent would save a reference to the parameter and clone it or create an instance of it in render()
.
But, if you did something like this, you would be messing up the "Unidirectional Data Flow" that makes React so powerful. By passing data and commands up and down, your app becomes less deterministic and harder to reason about. As your app becomes more complex, it's really hard to reproduce tricky bugs and add new features.
If you really really need this kind of functionality, I would suggest looking at a state management library like Redux or MobX. A warning, though: They have a pretty significant learning curve.
I recently migrated a project to use Redux with React, because I wanted cool features like you describe. I had written myself into an uncomfortable corner. It's so much better to work with the philosophical grain of any library/framework than against it.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install react-page-layout
First Step is that you have to create a layout component, this is a regular react component that has several slots where content can be injected. You define one or more slots by using the <Slot> component and giving them a name. In this case "main". You have to make your app layout aware. In order to do this you use the <LayoutProvider> component to let decendants know about the different layouts of your app. It requires that you specify its layouts prop with an object, where the keys are the names and the values are the layout components;. Note: Your page components don't have to be direct decendents of the <LayoutProvider>, in fact you probably will never use it this way. This is for illustration purposes only. This feature is what allows you to use this package with any routing library. Next you have to create pages. The pages provide the content for the different slots in your layout. To do this we use two components, <Page> and <Section>. You have to pass the layout property to the <Page>, so it knows what layout you want to use. Similarly each <Section> has a slot property that ties it to the slot in the layout for which it provides content. In this case since we only have one layout named public, and it only has one slot named it main we use those values.
First Step is that you have to create a layout component, this is a regular react component that has several slots where content can be injected. You define one or more slots by using the <Slot> component and giving them a name. In this case "main" import React, { Component } from 'react'; import { Slot } from 'react-page-layout'; class PublicLayout extends Component { render() { return ( <div> <header>Page Header</header> <Slot name="main" /> <footer>Page Footer</footer> </div> ); } } export default PublicLayout;
You have to make your app layout aware. In order to do this you use the <LayoutProvider> component to let decendants know about the different layouts of your app. It requires that you specify its layouts prop with an object, where the keys are the names and the values are the layout components; import React, { Component } from 'react'; import { render } from 'react-dom'; import { LayoutProvider } from 'react-page-layout'; import PublicLayout from './layouts/public'; import LoginPage from './pages/login'; // Create a map for all your layouts const layouts = { 'public': PublicLayout, }; class App extends Component { function render() { // Render your page inside // the layout provider return ( <LayoutProvider layouts={layouts}> <LoginPage /> </LayoutProvider> ); } } render(App, document.getElementById('root')); Note: Your page components don't have to be direct decendents of the <LayoutProvider>, in fact you probably will never use it this way. This is for illustration purposes only. This feature is what allows you to use this package with any routing library.
Next you have to create pages. The pages provide the content for the different slots in your layout. To do this we use two components, <Page> and <Section>. import { Page, Section } from 'react-page-layout'; class LoginPage extends Component { render() { return ( <Page layout="public"> <Section slot="main"> <h1> THIS IS THE PAGE CONTENT </h1> </Section> </div> ); } } You have to pass the layout property to the <Page>, so it knows what layout you want to use. Similarly each <Section> has a slot property that ties it to the slot in the layout for which it provides content. In this case since we only have one layout named public, and it only has one slot named it main we use those values.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page