dynamic forms in react
class App extends React.Component {
constructor(props) {
super(props);
this.state = { values: [] };
this.handleSubmit = this.handleSubmit.bind(this);
}
createUI(){
return this.state.values.map((el, i) =>
<div key={i}>
<input type="text" value={el||''} onChange={this.handleChange.bind(this, i)} />
<input type='button' value='remove' onClick={this.removeClick.bind(this, i)}/>
</div>
)
}
handleChange(i, event) {
let values = [...this.state.values];
values[i] = event.target.value;
this.setState({ values });
}
addClick(){
this.setState(prevState => ({ values: [...prevState.values, '']}))
}
removeClick(i){
let values = [...this.state.values];
values.splice(i,1);
this.setState({ values });
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.values.join(', '));
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
{this.createUI()}
<input type='button' value='add more' onClick={this.addClick.bind(this)}/>
<input type="submit" value="Submit" />
</form>
);
}
}
ReactDOM.render(<App />, document.getElementById('container'));
Also in JavaScript:
- javascrip check if string contains substring
- node sass
- checking if a character is an alphabet in js
- javascript display block
- compare two arrays javascript
- Itsycal homebrew
- chart js rotating the x axis labels
- display amount with currency for jquery
- lastindexof javascript
- do i have to include jquery files at head of bootstrap
- how to remove event listener in jquery
- Unterminated quote at columns 0-8 ['MM-yyyy] in expression ['MM-yyyy]
- node_modules/react-native-paper/lib/module/core/Provider.js
- js toggle boolean
- set readonly in javascript
- pd.read_json('data.json') args
- async map js
- sequelize mariadb example
- funcao com o botao esc do teclado javascript
- how to export fs.readFile
- coreui react change background color
- how to reload a module in node.js
- error: Error: Unable to resolve module `react-native-gesture-handler` from `node_modules\@react-navigation\native\lib\module\Scrollables.js`: react-native-gesture-handler could not be found within the project.
- angular copy