我想创建一个简单的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