Javascript 如何使html属性的存在成为动态的?
在下面的代码中,我想使Javascript 如何使html属性的存在成为动态的?,javascript,reactjs,Javascript,Reactjs,在下面的代码中,我想使输入的“checked”属性成为动态的。显然,completed是一个布尔值。编译在{check}的开头失败,错误为TS1005:“…”。 import React from 'react'; export class TodoListItem extends React.Component<any, any> { render() { const {label, completed} = this.props; const check =
输入的“checked”属性成为动态的。显然,completed
是一个布尔值。编译在{check}
的开头失败,错误为TS1005:“…”。
import React from 'react';
export class TodoListItem extends React.Component<any, any> {
render() {
const {label, completed} = this.props;
const check = completed? "checked": " ";
return (
<li className="todo">
<label>
<input type="checkbox" {check}/> {label}
</label>
</li>
);
}
}
从“React”导入React;
导出类TodoListItem扩展React.Component{
render(){
const{label,completed}=this.props;
常量检查=已完成?“已检查”:“;
返回(
{label}
);
}
}
您可以直接传递checked={completed},因此如果completed为true,则复选框将被选中,否则将被取消选中
import React from 'react';
export class TodoListItem extends React.Component<any, any> {
render() {
const {label, completed} = this.props;
return (
<li className="todo">
<label>
<input type="checkbox" checked={completed} /> {label}
</label>
</li>
);
}
}
从“React”导入React;
导出类TodoListItem扩展React.Component{
render(){
const{label,completed}=this.props;
返回(
{label}
);
}
}