React proptype array with shape

You can use React.PropTypes.shape() as an argument to React.PropTypes.arrayOf(): // an array of a particular shape. ReactComponent.propTypes = { arrayWithShape: React.PropTypes.arrayOf(React.PropTypes.shape({ color: React.PropTypes.string.isRequired, fontSize: React.PropTypes.number.isRequired, })).isRequired, } See the Prop Validation section of the documentation. UPDATE As of react v15.5, using React.PropTypes is deprecated and the standalone package prop-types should be used instead : // … Read more

Reactjs setState() with a dynamic key name?

Thanks to @Cory’s hint, i used this: inputChangeHandler : function (event) { var stateObject = function() { returnObj = {}; returnObj[this.target.id] = this.target.value; return returnObj; }.bind(event)(); this.setState( stateObject ); }, If using ES6 or the Babel transpiler to transform your JSX code, you can accomplish this with computed property names, too: inputChangeHandler : function (event) … Read more

How to use comments in React

Within the render method comments are allowed, but in order to use them within JSX, you have to wrap them in braces and use multi-line style comments. <div className=”dropdown”> {/* whenClicked is a property not an event, per se. */} <Button whenClicked={this.handleClick} className=”btn-default” title={this.props.title} subTitleClassName=”caret”></Button> <UnorderedList /> </div> You can read more about how comments … Read more

How to render HTML string as real HTML?

Is this.props.match.description a string or an object? If it’s a string, it should be converted to HTML just fine. Example: class App extends React.Component { constructor() { super(); this.state = { description: ‘<h1 style=”color:red;”>something</h1>’ } } render() { return ( <div dangerouslySetInnerHTML={{ __html: this.state.description }} /> ); } } ReactDOM.render(<App />, document.getElementById(‘root’)); Result: http://codepen.io/ilanus/pen/QKgoLA?editors=1011 However … Read more

How to access a DOM element in React? What is the equilvalent of document.getElementById() in React

You can do that by specifying the ref EDIT: In react v16.8.0 with function component, you can define a ref with useRef. Note that when you specify a ref on a function component, you need to use React.forwardRef on it to forward the ref to the DOM element of use useImperativeHandle to to expose certain … Read more