If-else in React Stateless functional components
By : naresh
Date : March 29 2020, 07:55 AM
around this issue I finally figured out the problem. I have to make 2 changes in my new code: 1) Use ternary operator for if-else code :
import React from "react";
import currentUser from "shared/components/CurrentUser";
import Home from "../Home";
import Landing from "../Landing";
const DefaultRouteHandler = ({currentUser}) => (
!currentUser ? <Landing /> : <Home />
);
export default currentUser(DefaultRouteHandler);
|
Helpers in stateless functional components
By : Luis Moncaleano
Date : March 29 2020, 07:55 AM
should help you out If you intend to reuse the helper function, put it outside the stateless component function, either in same file or another, and export it: code :
export const myHelper = (value) => {
// Insert logic
};
|
Is there a way to add children components to functional stateless components that are called as functions?
By : Hana Fetene
Date : March 29 2020, 07:55 AM
Does that help I want to call it as a function, yes you can. But then you will have to restructure your components and it will come at come cost. You cannot use them interchangeably without some compromises. Following is a sample: code :
const Parent = function(props){
return (
<div>
<h3> This is Parent</h3>
{
Array.isArray(props.children) ?
props.children.map(x => typeof(x) === 'function' ? x() : x) :
null
}
</div>
)
}
const Child = function(props){
return (
<p>This is Child! My name is {props.name}</p>
)
}
let JSX = Parent({
children: [
Child.bind(null, {name: 'foo'}),
Child.bind(null, {name: 'Bar'})
]
});
ReactDOM.render(JSX, document.getElementById('content'));
let JSX2 = (
<Parent>
<Child name='Jon'/>
</Parent>
)
ReactDOM.render(JSX2, document.getElementById('content2'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='content'></div>
<hr/>
<div id='content2'></div>
|
Why use stateless functional Components instead of class-based Components?
By : user2734766
Date : March 29 2020, 07:55 AM
this will help Functional state-less components (what wrongly you refer as UI Components, all components are UI components both statefull and stateless) are simply a short-hand method to create components that simply render something based on props passed and do not need to keep internal state. Of course one can always use class-based components which extend React.Component. But why not have a short-hand to save time and space and simplify things if we can. There is nothing forcing you to create functional components, you can always use class-based components, only if you need to simplify and save time and space.
|
Functional Stateless Components and Keys
By : user2797354
Date : March 29 2020, 07:55 AM
wish helps you How do you Map a key in a functional component, precisely the 'key' keyword, not just a general index? , Try something like that code :
const ComponentOne = ({records}) => (
<React.Fragment>
{records.map( (record, index) => <ComponentTwo data={record} key={index}/>)}
</React.Fragment>
);
|