ivan
ivan

Reputation: 638

How to read props expression found in React app

What does this.props["*"] return in context of React app? Is it React specific or just some JavaScript syntax?

Upvotes: 1

Views: 49

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 282030

this.props is an object and the above syntax is to access a particular key using brackets notation. The syntax is a pure Javascript syntax. It will return you the prop which was passed with key *

Sample demo

const obj = {
  "*": "12",
  first: 13,
  Second: 13
};

class Hello extends React.Component {
  render() {
    console.log(this.props["*"]);
    return (
      <div className="App">
        <h1>Hello CodeSandbox</h1>
        <h2>Start editing to see some magic happen!</h2>
      </div>
    );
  }
}
function App() {
  return <Hello {...obj} />;
}

ReactDOM.render(<App/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"/>

Upvotes: 1

Related Questions