I need to create a visual editor for an HTML page. ReactJS seems to be a good choice for this. I am currently facing the following problem:
I modeled my data:
var Model = { title: 'Hello', description: 'Pellentesque eleifend urna ac purus tempus...', date: new Date().toString() };
And the built-in component that stores its data inside the specified structure:
var App = React.createClass({ getInitialState: function () { return { value: Model } }, handleMouseEnter: function (event) { $(event.target).css('outline', '1px solid red').attr('contenteditable', true); }, handleMouseLeave: function (event) { var t = $(event.target), v = this.state.value; t.css('outline', 'none').attr('contenteditable', false); v[t.attr('property')] = t.text(); this.setState({value: v}); }, render: function () { var v = this.state.value; return ( <div> <pre style={{whiteSpace: 'normal'}}>{JSON.stringify(this.state)}</pre> <h1 onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave} property="title">{v.title}</h1> <p onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave} property="description">{v.description}</p> <p onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave} property="date">{v.date}</p> </div> ); } }); React.renderComponent(<App />, document.getElementById('app'));
In this particular case, this works. But I want to get rid of the onMouseEnter and onMouseLeave and leave only the property field. For instance:
<pre style={{whiteSpace: 'normal'}}>{JSON.stringify(this.state)}</pre> <h1 property="title">{v.title}</h1> <p property="description">{v.description}</p> <p property="date">{v.date}</p>
I was thinking about creating a mixin. Then, inside the componentDidMount event, attach handlers to all elements with the property attribute. But I did not find a way to achieve this.
My question is: Is there a way around the tree created by React? I noticed that React Developer Tools (Chrome Extension) can do this.
Similar question: React.js component creating parent child relations and iteration
source share