function Mailbox(props) {
const unreadMessages = props.unreadMessages;
return (
<div>
<h1>Hello!</h1>
{unreadMessages.length > 0 && <h2> You have {unreadMessages.length} unread messages. </h2> } </div>
);
}
const messages = ['React', 'Re: React', 'Re:Re: React'];
ReactDOM.render(
<Mailbox unreadMessages={messages} />,
document.getElementById('root')
);
render() {
const count = 0; return (
<div>
{ count && <h1>Messages: {count}</h1>} </div>
);
}
You can use conditional rendering with the logical &&
Operator.
You can read more about conditional renders in the Docs.
For example:
render() {
return (
SomeCondition &&
<Modal
title="Basic Modal"
visible={true}
onOk={this.handleOk}
onCancel={this.handleCancel}
>
<p>Some contents...</p>
{console.log('visible is false but content is being executed!')}
</Modal>
);
}
You can use Conditional Rendering.
import {addBot} from './actions';
class FlowsContainer extends React.Component {
componentDidMount() {
this.props.initStoreWithBot();
}
render() {
// *** at this point I have the store in state prop
//but editorFlow array is not yet instanced, it's undefined
const { editorFlow } = this.props.state;
let tasks;
if (typeof editorFlow === 'object' && editorFlow.length > 0) {
tasks = editorFlow[0].flow.tasks;
}
return (
{tasks &&
<div>
Flow editor react component in main container
</div>
}
);
}
}
const mapStateToProps = (state) => ({
state : state
})
const mapDispatchToProps = (dispatch) => {
return {
initStoreWithBot : () => dispatch(addBot("test 123"))
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(FlowsContainer)
{
isAllChecked && (
<HomenetText headingSmall>
<strong>Fortsatt uten nett?</strong>
</HomenetText>
)
}
In <TodoItem />
you can similar logic for modifing todo text as you use in <AddTodo />
for creating new todo. I use conditional rendering to render the <TextInput />
when isEditing
is true
, and <Text />
when it's not.
{isEditing
? <TextInput value={text} onChangeText={setText} style={styles.itemText} />
: <Text style={styles.itemText}>{props.title}</Text>
}
Actually you can do everything you did inside render()
method in class-base component with your JSX code.
You can use inline ternary operator, logic AND or logic OR operator or you can combine it using variables in your code:
You can see all three ways to do that in the example below:
function MyComponent () {
let somePart = null;
if (condition) {
somePart = <Third />;
}
return (
<Fragment>
<Top />
{showAll && <Middle />}
{showConditionally ? <First /> : <Second />}
{somePart}
<Bottom />
</Fragment>
);
}
Recommend
React Conditional Rendering Element Variables
React Composition vs Inheritance Specialization
React Composition vs Inheritance Containment
React Using the State Hook Recap Tip: Using Multiple State Variables
React Using the State Hook Recap Tip: What Do Square Brackets Mean?
React Using the State Hook Recap
React Using the State Hook Updating State
React Using the State Hook Reading State
React Using the State Hook Declaring a State Variable
React Using the State Hook What’s a Hook?
React Using the State Hook Hooks and Function Components
React Using the State Hook Equivalent Class Example
React Handling Events Passing Arguments to Event Handlers
React Rules of Hooks Explanation
React Rules of Hooks ESLint Plugin
ReactDOMServer Reference renderToStaticNodeStream()
ReactDOMServer Reference renderToNodeStream()
ReactDOMServer Reference renderToStaticMarkup()
ReactDOMServer Reference renderToString()
React Testing Recipes Multiple Renderers
React Testing Recipes Snapshot Testing
React Testing Recipes Mocking Modules
React Testing Recipes Data Fetching
React Testing Recipes Rendering
React Testing Recipes Setup/Teardown
React Rendering Elements Updating the Rendered Element
React Rendering Elements Rendering an Element into the DOM
React Component State What is the difference between passing an object or a function in setState?
React Component State Why is setState giving me the wrong value?
React Refs and the DOM Callback Refs
React Refs and the DOM Accessing Refs Refs and Function Components
React Refs and the DOM Accessing Refs Adding a Ref to a Class Component
React Refs and the DOM Accessing Refs Adding a Ref to a DOM Element
React Refs and the DOM Accessing Refs
React Refs and the DOM Creating Refs
File Structure Is there a recommended way to structure React projects? Grouping by file type
Glossary of React Terms Components props.children
Glossary of React Terms Components props
Glossary of React Terms Components
Glossary of React Terms Elements
React Code-Splitting Named Exports
React Code-Splitting Route-based code splitting
Code-Splitting React.lazy Error boundaries