reactjs - How To Create Dynamic Input box with label and Render Input value in ReactJs
问题描述
how to create Dynamic label And Input in On Click . Such As If I type label as Country Then user can input country name in input Box. and on submit And all the input value should populate with their label name , such as
Codesandbox Link: Dynamic form
example:
import React, { useState } from "react";
import ReactDOM from "react-dom";
import { ConProvider } from "../ContextApi";
import Data from "./Component/Data/Data";
import InputBox from "./InputBox";
import "./styles.css";
function App() {
const [input, setinput] = useState(["input-0"]);
const appendInput = (e) => {
// e.preventDefault();
var newInput = `input-${input.length}`;
setinput(input.concat([newInput]));
};
const handleInput = (i) => (e) => {
let inputs = [...input];
inputs[i] = e.target.value;
setinput(inputs);
};
console.log(input);
return (
<div className="App">
<Data />
<form>
{input.map((i, index) => (
<InputBox key={i} handleInput={handleInput} indexProps={index} />
))}
</form>
{input.map((i) => {
return <p key={i}>{i}</p>;
})}
<button onClick={appendInput}>Add Input</button>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(
<ConProvider>
<App />
</ConProvider>,
rootElement
);
解决方案
推荐阅读
- css - 在单击时使用变换比例
- .net - Clone a GIT repository with NGit in VB.net
- jmeter - 我将如何使用 Java 代码从 Jmeter API 使用 BackendListenerClient,如 GraphiteBackendListenerClient 和 InfluxdbBackendListenerClient?
- javascript - 为什么我无法获得数组长度
- python - 使用 Python 和 Fastload 实用程序将 csv 上传到 Teradata DB
- ios - 点击按钮时的图层
- c++ - 如何在优化模型之前访问 Gurobi 变量属性
- c# - 如何将datagridview旋转90度
- java - Maven javadoc插件仅修复现有的javadocs
- javascript - 在javascript中获取一年中的最后一个日历周