Cant text in input field - reactjs

I am building a meeting booking webb application. I want to save the content in the buttons to firebase and also the input text in the form to firebase.
I cant type text in the input field for then I get error: TypeError: Cannot read property 'refs' of undefined
enter image description here
enter image description here
import React, { Component } from "react";
import "./App.css";
import firebase from "firebase";
const uuid = require("uuid");
class App extends Component {
constructor(props) {
super(props);
this.state = {
uid: uuid.v1(),
meeting: "",
name: "",
email: ""
};
this.handleClick = this.handleClick.bind(this);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
console.log(firebase);
var database = firebase.database();
var ref = database.ref("meeting");
var data = {
id: "",
user: ""
};
ref.push(data);
// this.state = {
// items: [],
// isLoaded: true,
// }
// this.state = {
// name: '',
// email: '',
// };
}
handleClick = e => {
console.log(e.target.innerHTML);
alert("Du har valt ett möte");
};
componentDidMount() {
fetch("http://www.mocky.io/v2/5c9cdca03300004d003f2151")
.then(res => res.json())
.then(json => {
let meetings = [];
json.forEach(meeting => {
if (
new Date(meeting.startDate).getDay() !==
new Date(meeting.endDate).getDay()
) {
let day1 = {
activity: meeting.activity,
location: meeting.location,
startDate: meeting.startDate
};
let day2 = {
activity: meeting.activity,
location: meeting.location,
endDate: meeting.endDate
};
meetings.push(day1, day2);
} else {
meetings.push(meeting);
}
});
console.log(meetings);
this.setState({
isLoaded: true,
items: meetings
});
});
firebase
.database()
.ref(`Newdata/${this.state.uid}`)
.on("value", snap => console.log("from db", snap.val()));
}
handleChange(e) {
this.setState({
name: e.target.name
});
}
handleSubmit(e) {
alert("Er bokning är bekräftad: " + this.state.value);
console.log("Du har bekräftat er bokning");
e.preventDefault();
firebase
.database()
.ref(`Newdata/${this.state.uid}`)
.set({
meeting: this.state.meeting,
name: this.state.name,
email: this.state.email
})
.catch(error => console.log(error));
}
inputData(e) {
const meeting = this.refs.meeting1.value;
const name = this.refs.name1.value;
const email = this.refs.email1.value;
this.setState({ meeting, name, email });
}
render() {
var { isLoaded, items } = this.state;
if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<>
<div className="App">
<div className="AppHeader">
<h1>Boka ditt möte nedan</h1>
</div>
<ul>
{items.map((item, i) => (
<li key={i}>
<button
onClick={e => this.handleClick(e)}
onChange={this.inputData}
ref="meeting1"
className="select"
>
{item.activity}
<br />
Starttid: {item.startDate}
<br />
Sluttid: {item.endDate}
<br />
Plats: {item.location}
<br />
</button>
</li>
))}
</ul>
</div>
<div className="selectedMeeting">
Fyll i dina uppgifter och bekräfta
</div>
<form onSubmit={this.handleSubmit} className="bookingSection">
<label>
Name:
<input
type="text"
name={this.state.name}
onChange={this.inputData}
ref="name1"
/>
</label>
<label>
E-mail:
<input
type="text"
email={this.state.email}
onChange={this.inputData}
ref="email1"
/>
</label>
<input className="confirm" type="submit" value="Bekräfta" />
</form>
<div className="viewSelect" />
</>
);
}
}
}
export default App;

Make sure your "def" variable is defined before attempting to push it, like so:
var data = { id: "",user: "" };
var ref = [];
ref.push(...);

Either add to your constructor:
this.inputData = this.inputData.bind(this);
or use arrow syntax to preserve the lexical this:
inputData = (e) => {

Related

Onclick event not changing this.state.value2 variable

when the weather details are displayed after search, clicking on the image is supposed to append the details and display the retVal in the handleClick event. For some reason this.setState for value2 is not even changing the variable and keeping it null.Ive tried converting to async thinking that maybe setState hadnt been finished by the time the page rendered but that didnt work either.
class Search extends React.Component {
constructor(props) {
super(props);
this.state = {
city: "",
temp: 0,
feels: "",
w_status: "",
description: "",
condition: "",
max: 0,
min: 0,
wind_speed: 0,
humidity: "",
pressure: 0,
sunrise: 0,
lastUpdate: "",
image: "",
value1: null,
value2: "Value 2 is currently null",
activate_err: false,
};
this.handleClick = this.handleClick.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleClick(e) {
console.log("handle click is being called");
const retval = (
<div>
<p>Maximum Temprature: {this.state.max}</p>
<p>Minimum Temprature: {this.state.min}</p>
<p>Humidity: {this.state.humidity}</p>
<p>Wind Speed: {this.state.wind_speed}</p>
<p>Pressure: {this.state.pressure}</p>
<p>Sunrise: {this.state.sunrise}</p>
</div>
);
if(this.state.value2 != null){
this.setState({ value2: null});
console.log("It is hidden");
}
else{
this.setState({ value2: retval});
console.log("It is shown");
}
}
async handleSubmit(e) {
const search = e.target.SearchVal.value;
console.log("You entered " + search);
console.log("Time to modify the entry!");
e.preventDefault();
//Converts any string with extra spaces to one space in between
console.log(search.replace(/\s{2,}/g, " ").trim());
const results = search
.replace(/\s{2,}/g, " ")
.trim()
.split(" ");
await fetch(
"https://api.openweathermap.org/data/2.5/weather?q=" +
results +
"&appid=" +
KEY
)
.then((response) => response.json())
.then((data) => {
console.log(data);
if (data["cod"] != 200) {
console.log(
"Error during search, activate err has been set to true!"
);
this.setState({ activate_err: true });
} else {
this.setState({
city: data["name"],
temp: data["main"]["temp"],
feels: data["main"]["feels_like"],
w_status: data["weather"][0]["main"],
description: data["weather"][0]["description"],
max: data["main"]["temp_max"],
min: data["main"]["temp_min"],
wind_speed: data["wind"]["speed"],
humidity: data["main"]["humidity"],
pressure: data["main"]["pressure"],
sunrise: data["sys"]["sunrise"],
image:
"http://openweathermap.org/img/wn/" +
data["weather"][0]["icon"] +
"#2x.png",
activate_err: true,
});
const retval = (
<div>
<hr />
<div className="weather-card">
<h3>City: {this.state.city}</h3> <img onClick={() => this.handleClick()} src={this.state.image} />
<h3>
Temprature: {this.state.temp} Feels Like: {this.state.feels}
</h3>
<p>{this.state.w_status}</p>
<p>{this.state.description}</p>
<div id="details" className="weather-details">
{this.state.value2}
</div>
</div>
<hr />
</div>
);
this.setState({ value1: retval });
}
})
.catch((err) => {
console.log(err);
});
if (this.state.activate_err == false) {
}
/*
let status = true;
let activate = false;
let i = 0;
while(status){
if(result[i] == ' ')
status = false;
if(activate){
}
}
status = true;
*/
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<input
type="search"
placeholder="Enter a city, country or country code!"
name="SearchVal"
/>
<input type="submit" value="Search" />
</form>
<div id="search-results">{this.state.value1}</div>
</div>
);
}
}

React: loop through array response does not work

I run an axios get request to an Express server to get some dummy data:
customers = [
{ id: 1, firstName: "John" },
{ id: 2, firstName: "Brad" },
{ id: 3, firstName: "Mary" }
];
the data that is returned is determined by a user input: if the user enters "Brad", the result being returned will be { id: 2, firstName: "Brad" }.
For some reason, when I loop through the data returned, nothing works. Looking into the React Chrome Extension, the State returned is as follows:
{
"firstName": "Brad",
"id": 0,
"customers": [],
"res": []
}
Because I am learning React, I will post the whole component for you to see, perhaps the problem is something I don't see yet as a newbie:
class App extends Component {
constructor(props) {
super(props);
this.state = {
firstName: "",
id: 0,
customers: []
};
this.getName = this.getName.bind(this);
}
onChange = e => {
e.preventDefault();
this.setState({ firstName: e.target.value });
};
getName = e => {
e.preventDefault();
axios
.get("/api/customers", {
params: {
firstName: this.state.firstName
}
})
.then(res => {
console.log(res);
this.setState({ res: this.state.customers });
});
};
componentDidMount() {}
render() {
//if (!this.state.customers.length) return "Data not available yet";
return (
<div>
<h2>Customers</h2>
<div className="card card-body mb-4 p-4">
<div className="h1 display-4 text-center">
<h1 className="i fas">Search for a customer</h1>
<p className="lead text-center">Get the Customer's Name Here</p>
<form onSubmit={this.getName}>
<div className="form-group">
<input
type="text"
className="form-control form-control-lg"
placeholder="customer name ..."
name="firstName"
value={this.state.firstName}
onChange={this.onChange}
/>
</div>
<button
className="btn btn-primary btn-lg-block mb-5"
type="submit"
>
Get Customer
</button>
</form>
</div>
</div>
This is where I loop throuhg the data:
{this.state.customers.map(item => (
<li key={item.id}>{item.firstName}</li>
))}
</div>
);
}
}
And here is the Express route I'm calling:
app.get("/api/customers", (req, res) => {
let { firstName } = req.query;
const customers = [
{ id: 1, firstName: "John" },
{ id: 2, firstName: "Brad" },
{ id: 3, firstName: "Mary" }
];
console.log("the customer's name is " + firstName);
var str = customers.filter(x => x.firstName == firstName);
res.json(str);
});
So What I want is to display the data in a element below the input bar.
Try :
this.setState({ customers : res });
instead of :
this.setState({ res: this.state.customers });
Also, try to use React Hooks in order to refactor your code.

Save data to firebase

enter image description here
I am building this meeting booking app where the available meetings to books shows as buttons and after klicking the meeting you want so select. I want to make it possible to save that information in the button with a name and email that is written in the form.
I am having it hard to set the code so that my button selection is saved and saved to firebase along with the name and email after submit button is pressed. Right know I get the error that 'set' in handleSubmit is not set.
import React, { Component } from "react";
import "./App.css";
import firebase from "firebase";
const uuid = require("uuid");
class App extends Component {
constructor(props) {
super(props);
this.state = {
uid: uuid.v1(),
meeting: "",
name: "",
email: ""
};
this.handleClick = this.handleClick.bind(this);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
// console.log(firebase);
var database = firebase.database();
var ref = database.ref("meeting");
var data = {
id: "",
user: ""
};
ref.push(data);
// this.state = {
// items: [],
// isLoaded: true,
// }
// this.state = {
// name: '',
// email: '',
// };
}
handleClick = e => {
console.log(e.target.innerHTML);
alert("Du har valt ett möte");
};
componentDidMount() {
fetch("http://www.mocky.io/v2/5c9cdca03300004d003f2151")
.then(res => res.json())
.then(json => {
let meetings = [];
json.forEach(meeting => {
if (
new Date(meeting.startDate).getDay() !==
new Date(meeting.endDate).getDay()
) {
let day1 = {
activity: meeting.activity,
location: meeting.location,
startDate: meeting.startDate
};
let day2 = {
activity: meeting.activity,
location: meeting.location,
endDate: meeting.endDate
};
meetings.push(day1, day2);
} else {
meetings.push(meeting);
}
});
console.log(meetings);
this.setState({
isLoaded: true,
items: meetings
});
});
firebase
.database()
.ref(`Newdata/${this.state.uid}`)
.on("value", snap => console.log("from db", snap.val()));
}
handleChange(e) {
this.setState({
name: e.target.name
});
}
handleSubmit(e) {
alert("Er bokning är bekräftad: " + this.state.value);
console.log("Du har bekräftat er bokning");
e.preventDefault();
firebase.database().ref(`Newdata/${this.state.uid}`);
set({
meeting: this.state.meeting,
name: this.state.name,
email: this.state.email
}).catch(error => console.log(error));
}
inputData(e) {
const meeting = this.refs.meeting1.value;
const name = this.refs.name1.value;
const email = this.refs.email1.value;
this.setState({ meeting, name, email });
}
render() {
var { isLoaded, items } = this.state;
if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<>
<div className="App">
<div className="AppHeader">
<h1>Boka ditt möte nedan</h1>
</div>
<ul>
{items.map((item, i) => (
<li key={i}>
<button
onClick={e => this.handleClick(e)}
onChange={this.inputData}
className="select"
>
{item.activity}
<br />
Starttid: {item.startDate}
<br />
Sluttid: {item.endDate}
<br />
Plats: {item.location}
<br />
</button>
</li>
))}
</ul>
</div>
<div className="selectedMeeting">
Fyll i dina uppgifter och bekräfta
</div>
<form onSubmit={this.handleSubmit} className="bookingSection">
<label>
Name:
<input
type="text"
name={this.state.name}
onChange={this.inputData}
onChange={this.handleChange}
ref="name1"
/>
</label>
<label>
E-mail:
<input
type="text"
email={this.state.email}
onChange={this.inputData}
onChange={this.handleChange}
ref="email1"
/>
</label>
<input className="confirm" type="submit" value="Bekräfta" />
</form>
<div className="viewSelect" />
</>
);
}
}
}
export default App;
TL;DR;
You have a typo in your code, it should be:
firebase.database().ref('Newdata/${this.state.uid}').set({
meeting: this.state.meeting,
name: this.state.name,
email: this.state.email
}).catch(error => console.log(error));
Explanation:
Since you add the ;, you end the first expression and start a new one that is:
set({
meeting: this.state.meeting,
name: this.state.name,
email: this.state.email
}).catch(error => console.log(error));
Since there is no function defined, Javascript gives this error. But what you want do do is call the method set of the object firebase.database().ref('Newdata/${this.state.uid}'), therefore you should do:
firebase.database().ref('Newdata/${this.state.uid}').set({ /* ... */ })

Load data into inputs when entering the code

I have updated the Code.
Here I have a functional Select Autocomple showing the list of records from DB "Register". When selecting a Code, the Name value is automatically renamed.
The same thing I want to do but with the not with , I want to call more than two values like this in the image and in select is only Label and Value
Capture: [1]: https://i.stack.imgur.com/ELf1a.png
class Register extends Component {
state = {
status: "initial",
data: [],
name:'',
code:''
}
componentDidMount = () => {
this. getInfo()
}
getInfo= async () => {
try {
const response = await getAll('register')
console.log(response.data)
this.setState({
status: "done",
data: response.data
});
} catch (error) {
this.setState({
status: "error"
});
}
};
handleChange = (selectedOption) => {
this.setState({
selectedOption,
name: selectedOption.value
});
render() {
//show Name and code on Select from Register
const data = this.state.data.map( st => ({value: st.Name, label: st.Code}));
return (
<Container>
<RowContainer margin="1px" >
<ColumnContainer margin="10px">
<h3>Info</h3>
<label>Code</label>
<Select
width='215px'
value={selectedOption}
onChange={this.handleChange}
options={data}
name={"Code"}
/>
<label>Name</label>
<Input
width='150px'
type="text"
name={"Name"}
placeholder="Name"
value={this.state.name} />
</ColumnContainer>
</RowContainer>
</Container>
)
}
};
export default Register;
You want to know how change the state for <input/>
try this
constructor(props){
super(props)
this.state = {
status: "initial",
data: [],
codigo: "",
nombre: ""
}
}
handleChange(event){
let stateUpdate = this.state;
stateUpdate[event.target.name] = event.target.value}
this.setState(stateUpdate);
}
render() {
const data = [...this.state.data];
return (
<Container>
<RowContainer margin="1px" >
<ColumnContainer margin="10px">
<h3>Info</h3>
<label>Codigo</label>
<Input
name="codigo"
width='150px'
type="text"
placeholder="Digite el codigo"
value={data.codigo } ref="codigo" />
<label>Nombre</label>
<Input
name="nombre"
width='150px'
type="text"
placeholder="Nombre completo"
value={this.state.nombre} />
</ColumnContainer>
</RowContainer>
</Container>
)
}

Build form by using map(), how to put info from this.state?

import React, { Component } from "react";
import myPhone from "../service/checkPhone.js";
import {usersParam} from'../variable.js';
class FormForUserChange extends Component {
constructor() {
super();
this.state = {
name: "",
age: "",
gender: "",
phone: "",
address: "",
display: "none"
};
}
componentWillMount = () => {
this.setState({ name: this.props.userToChange.name });
this.setState({ age: this.props.userToChange.age });
this.setState({ gender: this.props.userToChange.gender });
this.setState({ phone: this.props.userToChange.phone });
this.setState({ address: this.props.userToChange.address });
};
_makeListFormData=(usersParam)=>{
return usersParam.map(each => {
return (
<input
className="form-control"
type="text"
// defaultValue={this.state.{each}}
placeholder={each}
ref={input => (this.each = input)}
/>
);
});
}
_handleChange = event => {
this.setState({ gender: event.target.value });
};
_handleSubmit = event => {
event.preventDefault();
if (
this.name.value &&
this.address.value &&
this.phone.value &&
this.age.value &&
myPhone(this.phone.value)
) {
const changedUser = {
name: this.name.value,
age: this.age.value,
gender: this.state.gender,
phone: this.phone.value,
address: this.address.value,
id: this.props.userToChange.ident
};
this.props.saveChangedUser(changedUser, this.props.userToChange.hash);
} else {
this.setState({ display: "block" });
}
};
render() {
let form;
let btnText;
const styles = {
display: this.state.display
};
const inputsInForm=this._makeListFormData(usersParam);
if (this.props.openModal) {
form = (
<div className="shadow p-3 mb-5 bg-white rounded" id="form">
<form
className="form-control-file. form-container"
onSubmit={this._handleSubmit.bind(this)}
>
{inputsInForm}
<button className="btn btn-primary" type="submit">
Save changes
</button>
</form>
<span id="form-fill-error" style={styles}>
please fill out all fields correct
</span>
</div>
);
} else {
form = "";
}
return (
<div>
<button
className="btn btn-primary"
id="add-user-btn"
disabled="disabled"
>
{btnText}
</button>
{form}
</div>
);
}
}
export default FormForUserChange;
I have an array from which I build inputs for form(_makeListFormData). In phraseholder I have to put info from state(which comes from props).
So in placeholder I should put something like this.state{each} it does't work off course. Can you give me an advise how to make it?
You can use placeholder={this.state[each]} for your situation. Also, use componentDidMount since componentWillMount will be deprecated in the future as #Think-Twice explained. Also, set your state in one time, not separately like that.
const usersParam = ['name', 'age', 'gender', 'phone', 'address'];
class FormForUserChange extends React.Component {
constructor() {
super();
this.state = {
name: "",
age: "",
gender: "",
phone: "",
address: "",
display: "none"
};
}
componentDidMount = () => {
this.setState({
name: this.props.userToChange.name,
age: this.props.userToChange.age,
gender: this.props.userToChange.gender,
phone: this.props.userToChange.phone,
address: this.props.userToChange.address,
});
};
_makeListFormData = (usersParam) => {
return usersParam.map(each => {
return (
<input
className="form-control"
type="text"
// defaultValue={this.state.{each}}
placeholder={this.state[each]}
ref={input => (this.each = input)}
/>
);
});
}
render() {
const inputsInForm = this._makeListFormData(usersParam);
return(
<div>{inputsInForm}</div>
);
}
}
const userToChange = {
name: "foo",
age: 25,
gender: "male",
phone: "123",
address: "some add",
}
ReactDOM.render(<FormForUserChange userToChange={userToChange} />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
This is not solution for your issue though but few issues I would like to address.
Stop using componentWillMount because this is deprecated.
so you can directly assign props in constructor
constructor(props) {
super(props);
this.state = {
name: this.props.userToChange.name,
age: this.props.userToChange.age,
gender: this.props.userToChange.gender,
phone: this.props.userToChange..phone,
address: this.props.userToChange.address,
display: "none"
};
}
Note: you no need to use setState for each. You can do everything thing in single setState like below
this.setState({
name: this.props.userToChange.name,
age: this.props.userToChange.age,
gender: this.props.userToChange.gender,
phone: this.props.userToChange..phone,
address: this.props.userToChange.address,
display: "none"
});

Resources