Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional mixins in React?

Tags:

reactjs

Is it possible to build the mixin array for a React component based on a prop?

I have one component with a prop that may take one of two values. Based on this, I need radically different behavior from one of my mixins. I'd like to separate this mixin into two separate files rather than having monstrous conditional statements everywhere, but I can't find an easy way to do this. Any tips?

like image 861
Bonnie Avatar asked Oct 15 '14 12:10

Bonnie


People also ask

What are Mixins in React?

React Developers introduced 'Mixin' system as an intermediate stage of adoption of new patterns. Since then, Vue. js, Angular and other component model-driven frameworks fulfilled the niche. Declarative UI using Composition over Inheritance is no longer novelty.

What is a conditional statement in React?

Conditional rendering is a term to describe the ability to render different user interface (UI) markup if a condition is true or false. In React, it allows us to render different elements or components based on a condition.

How do you write if condition in JSX?

We can embed any JavaScript expression in JSX by wrapping it in curly braces. But only expressions not statements, means directly we can not put any statement (if-else/switch/for) inside JSX.

What is PureRenderMixin?

PureRenderMixin. React provides the official PureRenderMixin plugin, which makes the shouldComponentUpdate function return False. The plugin can reduce unnecessary re-rendering and improve performance to a certain extent.


Video Answer


1 Answers

No, it's not possible. Without knowing anymore, it sounds like you might want to explore the idea of creating different components and conditionally using the right one from the parent. You could even wrap this up and hide that from consumers if you want.

like image 161
Paul O'Shannessy Avatar answered Oct 10 '22 10:10

Paul O'Shannessy