HTML 페이지의 비주얼 편집기를 만들어야합니다. ReactJS가 좋은 선택 인 것 같습니다. 현재 나는 다음과 같은 문제에 직면 : 위의 구조 내에서 데이터를 저장React가 만든 트리를 가로 지르는 방법이 있습니까?
var Model = {
title: 'Hello',
description: 'Pellentesque eleifend urna ac purus tempus...',
date: new Date().toString()
};
그리고 내장 된 구성 요소 : 그것이 작동하는 특정 경우
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'));
내가 내 데이터를 모델링. 하지만
onMouseEnter
및
onMouseLeave
속성을 없애고
property
필드 만 남기를 원합니다. 좋아요 :
<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>
믹스 인을 만들려고 생각했습니다. 그런 다음 componentDidMount
이벤트 내에서 property
속성을 가진 모든 요소에 핸들러를 연결하십시오. 그러나 나는 이것을 달성 할 수있는 방법을 찾지 못했습니다.
내 질문 : React가 만든 트리를 트래버스하는 방법이 있습니까? 나는 개발자 도구 (Chrome 확장 프로그램)을 사용할 수 있음을 확인했습니다.
비슷한 질문 : React.js component creating parent child relations and iterating
그래서 부모가 생성 한 하위 구성 요소를 트래버스하는 방법이 필요합니까? 부모님은 모델 배열을 저장하는 주를 보내십시오. 그런 다음 렌더링에서''array.map'''을 사용하여 배열의 각 모델을 반복하고 eventHandlers와 부모 모델 데이터를 전달하는 자식 구성 요소를 @trekforever 나쁜 해결책 인 – trekforever
에서 반환 할 수 있습니다. 그것으로 나는 하나의 모델을 더 가질 필요가있다 - DOM 노드가 생성 될 뷰를 위해. http : // jsfiddle.net/kb3gN/6684/ – vbarbarosh
필자가 설명하는 것을 왜하고 싶은지 혼란 스럽다. 정확히 'array.map'을 사용하여 달성 할 수없는 일을 정확히하려는 것일까? –