React CS tryout
Showing how to build HTML dynamically
by Ben Clayton
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app,#app2 {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
.done {
color: rgba(0, 0, 0, 0.3);
text-decoration: line-through;
}
input {
margin-right: 5px;
}
React
// https://zhenyong.github.io/react/docs/more-about-refs.html
var vm=[];
class Base extends React.Component {
constructor(props) {
super(props)
}
}
class Heading extends Base {
constructor(props) {
super(props)
this.state = {
items: [ ]
}
debugger
this.text = "Loading"
vm[props.bidx] = this.myRef
console.log(this.myRef)
}
init(data){
debugger
this.data = data
this.text = this.data.text
}
render() {
return (
<div>
<h1>ZZ
{this.text}
</h1>
</div>
)
}
}
class Editor extends React.Component {
constructor(props) {
super(props)
debugger
this.data = JSON.parse(props.jdata)
// <Heading text='[component h1]' />
// <SubHeading text='[component h2]' />
this.jdata = JSON.stringify(this.data)
// jdata={{this.jdata}}
this.block = <Heading bidx="p-0" ref={(ref) => this.myRef = ref} />
console.log(this.myRef)
}
render() {
return (
<div>
<h2>Editor Toolbar</h2>
{this.block}
</div>
)
}
}
ReactDOM.render(
<Editor jdata='{"type":"Heading","text":"Hello World"}' ref={(ref) => this.myRef = ref} />,
document.querySelector("#app")
);