Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="//fb.me/react-with-addons-0.12.0.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<script src="https://fb.me/react-15.1.0.js"></script>
<script src="https://fb.me/react-dom-15.1.0.js"></script>
</body>
</html>
 
.field-editor {
  margin: 1rem;
  
}
 
class FieldEditor extends React.Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
  }
  handleChange(event) {
    const text = event.target.value;
    this.props.onChange(this.props.id, text);
  }
  render() {
    return (
      <div className="field-editor">
        <input onChange={this.handleChange} value={this.props.value} />
      </div>
    );
  }
}
class FormEditor extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
    this.handleFieldChange = this.handleFieldChange.bind(this);
  }
  handleFieldChange(fieldId, value) {
    this.setState({ [fieldId]: value });
  }
  render() {
    const fields = this.props.fields.map(field => (
      <FieldEditor
        key={field}
        id={field}
        onChange={this.handleFieldChange}
        value={this.state[field]}
      />
    ));
    return (
      <div>
        {fields}
        <div>{JSON.stringify(this.state)}</div>
      </div>
    );
  }
}
// Convert to class component and add abillity to dynamically add/remove fields by having it in state
const App = () => {
  const fields = ["field1", "field2", "anotherField"];
  return <FormEditor fields={fields} />;
};
ReactDOM.render(<App />, document.body);
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers