有一个问题,我无法从选择状态中保存选择的值中获取选择的值,如何在父组件的状态中保存选择的值。 CHILS组件的选定值的状态存在于Component的状态
父组件
interface AddUserDataState {
UserName: string;
Name: string;
CompanyID: string;
CustomerID: string;
Email: string;
VAdministrador: string;
VCustomer: string;
VClient: string;
VMovil: string;
}
export default class AddUsers extends React.Component<RouteComponentProps<{}>,AddUserDataState>{
constructor(props: RouteComponentProps<{}>) {
super(props);
this.state = {
UserName:"",
Name: "",
CompanyID: "",
CustomerID: "",
Email: "",
VAdministrador: "",
VCustomer: "",
VClient: "",
VMovil: "",
}
this.updateInputUserName = this.updateInputUserName.bind(this);
this.updateInputName = this.updateInputName.bind(this);
this.updateInputCompanyID = this.updateInputCompanyID.bind(this);
//this.updateInputCustomerID = this.updateInputCustomerID.bind(this);
this.updateInputEmail = this.updateInputEmail.bind(this);
this.updateInputVAdministrador = this.updateInputVAdministrador.bind(this);
this.updateInputVCustomer = this.updateInputVCustomer.bind(this);
this.updateInputVClient = this.updateInputVClient.bind(this);
this.updateInputVMovil = this.updateInputVMovil.bind(this);
//this.onChangeCustomerID = this.onChangeCustomerID.bind(this);
}
updateInputUserName(UserName: string) {
this.setState({ UserName: UserName });
}
updateInputName(Name: string) {
this.setState({ Name: Name });
}
updateInputCompanyID(CompanyID: string) {
this.setState({ CompanyID: CompanyID });
}
/*updateInputCustomerID(CustomerID: string) {
this.setState({ CustomerID: CustomerID });
}*/
updateInputEmail(Email: string) {
this.setState({ Email: Email });
}
updateInputVAdministrador(VAdministrador: string) {
this.setState({ VAdministrador: VAdministrador });
}
updateInputVCustomer(VCustomer: string) {
this.setState({ VCustomer: VCustomer });
}
updateInputVClient(VClient: string) {
this.setState({ VClient: VClient });
}
updateInputVMovil(VMovil: string) {
this.setState({ VMovil: VMovil });
}
componentDidUpdate() {
console.log(this.state);
}
public FuncCreateUser(username: string, name: string, email: string, companyid: string, customerid: string, vadministrador: string, vcustomer: string, vclient: string, vmovil:string) {
const url = 'api/User/AddUser';
axios(url, { params: { username: username, name: name, email: email, companyid: companyid, customerid: customerid, vadministrador: vadministrador, vcustomer: vcustomer, vclient: vclient, vmovil: vmovil } })
.then(response => {
console.log(response.data);
alert(response.data);
});
}
public render() {
return <div className="col-md-8 order-md-1">
<h2 className="mb-3">Users</h2>
<form className="needs-validation">
<div className="row">
<div className="col-md-6 mb-3">
<label>UserName</label>
<input type="text" className="form-control" id="firstName" placeholder="" value={this.state.UserName} onChange={e => this.updateInputUserName(e.target.value)} />
</div>
<div className="col-md-6 mb-3">
<label>Name</label>
<input type="text" className="form-control" id="lastName" placeholder="" value={this.state.Name} onChange={e => this.updateInputName(e.target.value)} />
</div>
</div>
<div className="mb-3">
<label>Email <span className="text-muted">(Optional)</span></label>
<input type="email" className="form-control" id="email" placeholder="you@example.com" value={this.state.Email} onChange={e => this.updateInputEmail(e.target.value)} />
</div>
<div className="mb-3">
<label>Company#</label>
<input type="text" className="form-control" id="address" placeholder="1234 Main St" value={this.state.CompanyID} onChange={e => this.updateInputCompanyID(e.target.value)} />
</div>
<div className="mb-3">
<label>Customer#<span className="text-muted">(Optional)</span></label>
<DropDownListCustomer />
</div>
<div className="row">
<div className="col-md-3 mb-3">
<label>V Admin</label>
<input type="text" className="form-control" id="country" placeholder="" value={this.state.VAdministrador} onChange={e => this.updateInputVAdministrador(e.target.value)} />
</div>
<div className="col-md-3 mb-3">
<label>V Customer</label>
<input type="text" className="form-control" id="city" placeholder="" value={this.state.VCustomer} onChange={e => this.updateInputVCustomer(e.target.value)} />
</div>
<div className="col-md-3 mb-3">
<label>V Cliente</label>
<input type="text" className="form-control" id="zip" placeholder="" value={this.state.VClient} onChange={e => this.updateInputVClient(e.target.value)} />
</div>
<div className="col-md-3 mb-3">
<label>V Movil</label>
<input type="text" className="form-control" id="zip" placeholder="" value={this.state.VMovil} onChange={e => this.updateInputVMovil(e.target.value)} />
</div>
</div>
<hr className="mb-4" />
<button className="btn btn-primary btn-lg btn-block" type="button" onClick={() => { this.FuncCreateUser(this.state.UserName, this.state.Name, this.state.Email, this.state.CompanyID, this.state.CustomerID, this.state.VAdministrador, this.state.VCustomer, this.state.VClient, this.state.VMovil) }}>Create</button>
</form>
</div>
}
}
子组件 有一个问题,我无法从选择状态中获取所选值并将其保存到他的状态,如何保存在父组件状态中所选的值。 CHILS组件的选定值的状态存在于Component的状态
interface CustomerListModel {
results: Customer[];
}
interface Customer {
enter code here
customerId: number;
name: string;
addressId: number;
}
interface CustomerProps {
//CustomerID: string,
//onChangeCustomerID: () => {}
}
interface CustomerState {
CustomerList: CustomerListModel | null;
SelectedCustomer: string | "";
}
class DropDownListCustomer extends React.Component<CustomerProps, CustomerState>
{
constructor(props: CustomerProps) {
super(props);
this.state = {
CustomerList: null,
SelectedCustomer:""
}
}
getCustomerList = () => {
const url = "api/User/Get_All_Customer";
fetch(url)
.then(response => {
return response.json();
})
.then(response => {
this.setState({ CustomerList: response });
});
}
updateSelectedCustomer(selectedCustomer: string) {
this.setState({ SelectedCustomer: selectedCustomer })
}
public render() {
let { CustomerList } = this.state
return (
<select className="DropDownListCustomer form-control" value={this.state.SelectedCustomer} onChange={e => this.updateSelectedCustomer(e.target.value)}>
{
CustomerList && CustomerList.results.map(customer => {
return (
<option key={customer.customerId} value={customer.customerId}>{customer.name}</option>
)
})
}
</select>
)
}
componentDidMount() {
this.getCustomerList();
}
}
export default DropDownListCustomer
答案 0 :(得分:0)
客户的状态必须在您的父母中。首先将更新客户功能添加到您的父级:
updateSelectedCustomer(selectedCustomer: string) {
this.setState({ SelectedCustomer: selectedCustomer })
}
然后使用道具将该函数传递给您的孩子,并在发生更改时调用它:
interface CustomerProps {
...
updateSelectedCustomer: (selectedCustomer: string) => void
}
updateSelectedCustomer(selectedCustomer: string) {
this.props.updateSelectedCustomer(selectedCustomer)
}