我想創建一個簡單的Wizard
組件。我不知道如何綁定handleChange
函數onChange
輸入事件以及如何進入傳遞的上下文(這是我的自定義類在某處定義並在ParentClass
中實例化)。React模板中的綁定函數
Uncaught SyntaxError: Unexpected token }
這裏是我的Wizard
組件和簡單的模板,我創建的測試:
export class ParentClass extends React.Component {
render() {
let template = `<input value=${context.testVal} onChange=${context.handleChange.bind(context, event)}>`; // how to invoke this code inside Wizard using TestContext class???
let testContext = new TestContext();
return (
<Wizard template={template} context={testContext} />
);
}
}
export class TestContext {
testVal = null;
constructor() {
this.testVal = 10;
}
handleChange(e) {
console.log(e);
}
}
export class Wizard extends React.Component {
context: null;
constructor(props) {
super(props);
this.context = this.props.context;
}
render() {
return (
<div dangerouslySetInnerHTML={{__html: this.props.template}}>
</div>
);
}
}
我用ES2015
和Babel
編譯。
[編輯]
我修改了代碼和問題。我現在看到你們的意思是說「刪除$」。
你不理解我。我想聲明HTML
代碼與一些變量綁定(作爲一個字符串)+一些上下文類應該包含所有聲明模板的邏輯。當我有這些時,我想將它們作爲參數傳遞到Wizard
,並使用該模板替換此Wizard
的HTML
(同時執行JSX
)。換一種說法。我想在Wizard
組件中使用動態模板的通用機制。
只要刪除'' - 在這個意義上不需要模板 - 只需傳入'' – Chris
@Chris是對的,顯然你也必須刪除'$'標誌。 –
請看看我編輯的問題。 – Nickon