ReactJs How to create Auxiliary (Higher Order Component) in preact? reactjs create app,reactjs create element,reactjs create component,reactjs

I am trying to build an application using preact and I have created a simple component known as LayoutComponent.

In Layout component I have used Auxilary component as shown below:

import { h } from 'preact';
import Aux from '../../hoc/Aux';
const Layout = (props) => (
    <Aux>
        <div>
        Toolbar, Sidebar, Backdrop
        </div>
        <div>
            { props.children }
        </div>
    </Aux>
);
export default Layout;

And below mentioned is my Auxilary component code:

const Aux = ( props ) => props.children;
export default Aux;

When I am invoking LayoutComponent from my App.js render() method, the JSX content inside tag is not getting displayed.

If I replace the tag with tag then it is working fine, but I don't need that extra div in my DOM structure.

The DOM is shown something like this in chrome if I use Aux tag. enter image description here

Please Help.

Answer:1

The problem is that a component must return exactly one direct child. In React, this was solved recently by introducing Fragments, but this feature is currently not supported in Preact yet. If you are sure that the component will only have one child, you could insert it like this:

const Aux = (props) =>
  props.children.length === 1 && props.children[0];

However, since you want to insert two children in your example, the only way is indeed to wrap them in a div or any other tag like so:

const Aux = (props) => (
  <div>
    { props.children }
  </div>
);
Answer:2



  1. reactjs create app
  2. reactjs create element
  3. reactjs create component
  4. reactjs create array
  5. reactjs create map
  6. reactjs create object
  7. reactjs create json object
  8. reactjs create array of objects

I have an application where you arrive at a photo album listing via various means (searching, drill down by category, etc). From the album, you click open individual photos and iterate through them ...

I have an application where you arrive at a photo album listing via various means (searching, drill down by category, etc). From the album, you click open individual photos and iterate through them ...

onPress of login button, I want to navigate to some other screen basically a home screen with authentication taking username and password. I am using a stack navigator but when I click on login ...

onPress of login button, I want to navigate to some other screen basically a home screen with authentication taking username and password. I am using a stack navigator but when I click on login ...