reactjs.net을 사용하여 서버 측 구성 요소를 렌더링하고 있습니다. 내 문제는, 나는 ajax 호출을 통해 구성 요소와 해당 자바 스크립트에 대한 HTML 문자열을 가져오고 구성 요소를 렌더링하라는 요청을 받았습니다.서비스 측 렌더링 된 구성 요소에 대한 이벤트 바인딩이 없습니다.
이것은 HTML 문자열을 제공하는 나의 방법입니다.
public JsonResult GetComponent(string strComponentName)
{
string strHtml = "";
IHtmlContent htmlc= HtmlHelperExtensions.React(null, String.Format("Components.{0}", strComponentName), new { });
strHtml = GetString(htmlc);
IHtmlContent jscript = HtmlHelperExtensions.ReactInitJavaScript(null);
string strJavascript = GetString(jscript);
jsonresponse jr = new jsonresponse();
jr.html = strHtml;
jr.javascript = strJavascript;
return Json(jr);
}
이제이 기능은 몇 개의 버튼을 클릭하면 발생합니다. 수신 된 문자열이 렌더링 된 후. 렌더링 구성 요소에 대한 이벤트가 작동하지 않습니다
render() {
if (!this.state.rendered) {
this.fetchComp();
}
return (<div>
<div dangerouslySetInnerHTML={{ __html: this.state.html+" <script src=\"~/dist/client.bundle.js\"></script>" +this.state.javascript }} />
</div>);
}
을 다음과 같이
fetchComp() {
let FETCH_URL: string = "/Home/GetComponent?strComponentName=" + this.props.componentName;
fetch(FETCH_URL, {
method: 'GET'
})
.then(response => response.json())
.then(json => {
console.log("json from wrapper", json);
const html = json.html;
const javascript = json.javascript;
this.setState({ html: html, javascript: javascript,rendered: true });
console.log("state val", this.state);
});
}
렌더링 방법이다. javascript 필수 이벤트가 구성 요소와 함께 렌더링 되었더라도.
편집 : 스크립트 요소를 만들고 document.body에 추가하여 스크립트를 본체에 추가하려고 시도했습니다. 당신이 setState를 메소드가 fetchComp 함수를 호출하는이 부분에서
직접 상태를 설정하지 않습니다. –
처음에는 '렌더링 됨'상태가 false로 설정됩니다. 따라서 초기로드 중에 fetch 호출을 수행하는 'fetchComp'메소드를 호출합니다. 거기서 상태를 데이터로 설정하고 'rendered'상태를 true로 만듭니다. 이 setState는 렌더링에서 직접 완료됩니다. 무한 루프가 발생합니다. 콘솔 확인 :-) –
예. 그러나 그것은 문제가 아닙니다. 올바르게 렌더링됩니다. 렌더링 후에는 렌더링 된 구성 요소의 clickevent가 실행되지 않습니다. –