Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find React JSX element with a given property?

I'm doing an audit in a large codebase, and I need to search to find all uses of a component where it is used with a given prop. I'm thinking a regex could be useful here, but I can't figure out how to handle potential newlines in the markup. I need to be able to differentiate between these two usages, finding the latter:

<Component
  prop1="value1"
  prop2={2}
/>
<Component
  prop1="value1"
  targetProp={3}
  prop2={2}
/>

I don't care about the value of the target prop, just that it exists on the component.

like image 291
dangerismycat Avatar asked Mar 05 '19 21:03

dangerismycat


People also ask

What is JSX element []?

JSX allows us to write HTML elements in JavaScript and place them in the DOM without any createElement() and/or appendChild() methods. JSX converts HTML tags into react elements. You are not required to use JSX, but JSX makes it easier to write React applications.

How do I pass JSX in props?

You want to use JSX inside your props You can simply use {} to cause JSX to parse the parameter. The only limitation is the same as for every JSX element: It must return only one root element.

How do you use expressions in JSX?

Embedding Expressions in JSXYou can put any valid JavaScript expression inside the curly braces in JSX. For example, 2 + 2 , user.firstName , or formatName(user) are all valid JavaScript expressions. In the example below, we embed the result of calling a JavaScript function, formatName(user) , into an <h1> element.

Is JSX element A React node?

It is an object with type , props and key . JSX. Element is ReactElement , whose props and type have type any , so they are more or less the same. const jsx = <div>hello</div> const ele = React.


1 Answers

<Component(\s|\n)[^>]*?property

This one support line break.

like image 110
Sarawut Positwinyu Avatar answered Oct 04 '22 01:10

Sarawut Positwinyu