Hello I have a question about my code.
In the SearchBar.js, I got error, which is undefined props.
I have two question, if it is undefined why the if statement does not invoke and
I don't understand why props is undefined because I think I pass props properly from parent component to child component( SearchBar.js)
enter code here
import React from 'react';
import ReactDOM from 'react-dom';
import SearchBar from './components/SearchBar';
import ShopList from './components/ShopList';
class App extends React.Component {
state ={ title: [], text: ''}
componentDidMount(){
this.setState({title:["javascript","react","redux","c++","java"]})
}
handleSubmit = (e) =>{
e.preventDefault();
}
handleChange = (e) => {
this.setState({text: e.target.value})
}
render() {
return (
<div>
<div className="split left">
<div className="centered">
<SearchBar handleSubmit={this.handleSubmit} handleChange={this.handleChange} userinput={this.state.text}/>
<ShopList shops={this.state.title}/>
</div>
</div>
<div className="split right">
<div className="centered">
</div>
</div>
</div>
)
}
}
ReactDOM.render(<App />, document.querySelector('#root'));
SearchBar.js
import React from 'react';
class SearchBar extends React.Component{
constructor(props){
super(props);
}
render(){
if(!this.props){
return <div>Loding..</div>
}
return (
<form className="ui form" onSubmit={this.props.handleSubmit}>
<div className="inline field">
<input onChange={this.props.handleChange} className="myinput" type="text" style={{width: '85%'}} value={props.userinput}/>
<button className="ui primary button" type="submit">button</button>
</div>
</form>
)
}
}
export default SearchBar;
Related
i'm getting the following error while trying to search. I'm using redux to hold my state. Everytime i navigate away from the following page and back to it i get the error.
"Uncaught TypeError: Cannot read properties of undefined (reading 'toLowerCase')"
the following is my insurance component
import React, { Component } from 'react';
import { connect } from "react-redux";
import { fetchAllInsurances } from '../../actions/insurance.action';
import { Link } from 'react-router-dom';
import Card from '../Card';
import SearchBar from '../SearchBar';
import Header from '../Header';
export class Insurance extends Component {
constructor(props){
super(props);
this.state = {sCode:null};
}
onSearchSubmit = (term) => {
this.setState({sCode:term})
}
componentDidMount(){
this.props.fetchAllInsurances();
}
render() {
return (
<>
<Header/>
<div>
<SearchBar onSubmit={this.onSearchSubmit}/>
</div>
<br></br>
{this.renderCreate()}
<br></br>
<br></br>
<br></br>
{this.renderCard()}
</>
)
}
renderCreate(){
return(
<>
<Link to ={'/addinsurance'} className = "ui primary button">Add Insurance</Link>
</>
)
}
renderCard(){
return(
<div className="ui four column grid">
{this.props.insurance.filter(((c)=>{
if(this.state.sCode == null){
return c;
}
else if(c.VendorName.toLowerCase().includes(this.state.sCode.toLowerCase().toString())){
return c
}
})).map((c, i) => (
<Card key={i} InsData={c} />
))}
</div>
)
}
}
const mapStateToProps = state =>{
return{
insurance:Object.values(state.insurance)
}
}
export default connect(mapStateToProps,{fetchAllInsurances})(Insurance);
the following is my search component
import React from 'react'
class SearchBar extends React.Component {
state = {term:''};
onFormSubmit =(event) =>{
event.preventDefault();
//send the state data to the parent
this.props.onSubmit(this.state.term);
}
render(){
return (
<div className='ui segment'>
<form onSubmit = {this.onFormSubmit} action="" className="ui form">
<div className="field">
<label>Insurance Vendor Search</label>
<div className="searchInputs">
<input
type="text"
value={this.state.term}
onChange={e=> this.setState({term:e.target.value})}
placeholder='Enter the insurance vendor to search...'/>
</div>
</div>
</form>
</div>
)
}
}
export default SearchBar;
As the error state, the variable is undefined. You will need to test the variable first.
So either use
c.VendorName?.toLowerCase()
or
if (c.VendorName) {
c.VendorName.toLowerCase()........
}
I have a switch button that I want to copy ten times but when I copy the button to new div it does not let me click on second button onwards, I can change state of one first button only. Here is my code
class App extends Component {
handleSwitch = e => console.log(e.target.checked);
render() {
return (
<div>
<div>
<p className="title">source</p>
<label className="label">Button</label>2
<SwitchButton
onChange={this.handleSwitch}
title="SwitchButton"
data-name="vd"
/>
</div>
<div>
<label className="label">Button2</label>
<SwitchButton
onChange={this.handleSwitch}
title="SwitchButton2"
data-name="vd1"
/>
</div>
</div>
);
}
}
here is my live demo : https://stackblitz.com/edit/react-ts-9n4lwa?file=index.tsx
You should set a unique switchButtonID in each SwitchButton
import React, { Component } from 'react';
import { render } from 'react-dom';
import SwitchButton from './Hello';
import './style.scss';
class App extends Component {
handleSwitch = e => console.log(e.target.checked);
render() {
return (
<div>
<div>
<p className="title">Precursor source</p>
<label className="label">Ch1_PulsingValveActivationlnSw</label>
<SwitchButton
switchButtonID={1}
onChange={this.handleSwitch}
title="SwitchButton"
data-name="vd"
/>
</div>
<div>
<label className="label">Ch1_PulsingValveActivationlnSw</label>
<SwitchButton
switchButtonID={2}
onChange={this.handleSwitch}
title="SwitchButton2"
data-name="vd1"
/>
</div>
</div>
);
}
}
render(<App />, document.getElementById('root'));
I'm setting up a multipage form and have the steps assigned to an array.
What I am trying to figure out is the best way to add a new page to the form on button press. My solution was to build the new pages of the form (there will be 20 duplicates though, each with own variables).
Then I have a button labeled 'Add New' which calls a function to add a new line to the array, thus enabling the next page.
Of course, ideally my first solution was to have react automatically build a new page and variables but I'm not sure if that's feasible for me to do.
steps.js:
import React from 'react'
import { StepOne } from './StepOne'
import { StepTwo } from './StepTwo'
import { StepThree } from './StepThree'
const steps =
[
{name: 'Job', component: <StepOne/>},
{name: 'device 1', component: <StepTwo/>},
]
export { steps }
home.js (where my form lives)
import React, { Component } from "react";
import Button from 'react-bootstrap/Button';
import Form from 'react-bootstrap/Form';
import MultiStep from 'react-multistep'
import { steps } from './steps'
import {
Route,
NavLink,
HashRouter
} from "react-router-dom";
import newdevice from "./new-device";
class Home extends Component {
render() {
return (
<div>
<h1>Start a New Job</h1>
<div className='container'>
<div>
<MultiStep steps={steps} />
</div>
<div className='container app-footer'>
<h6>Press 'Enter' or click on progress bar for next step.</h6>
</div>
</div>
</div>
);
}
}
export default Home;
StepTwo.js
import React from 'react'
import { steps } from './steps'
import { StepThree } from './StepThree'
import update from 'react-addons-update';
export class StepTwo extends React.Component {
addNew() {
this.setState(previousState => ({
steps: [...previousState.steps, "{name: 'device 2', component: <StepThree/>},"]
}));
}
<div className='row'>
<div className="col-md-4">
<button onClick={this.addNew} variant="primary">Add a New Device</button>
</div>
</div>
</div>
)
}
}
If you want to see the entire page of the form for a better idea, full StepTwo.js
import React from 'react'
import { steps } from './steps'
import { StepThree } from './StepThree'
import update from 'react-addons-update'; // ES6
//
export class StepTwo extends React.Component {
addNew() {
this.setState(previousState => ({
steps: [...previousState.steps, "{name: 'device 2', component: <StepThree/>},"]
}));
}
constructor () {
super()
this.state = {
Box: '',
VIN: '',
Lbl: '',
Year: '',
Make: '',
Model: '',
Plate: '',
ODO: '',
Notes: '',
}
this.handleBoxChanged = this.handleBoxChanged.bind(this);
this.handleVINChanged = this.handleVINChanged.bind(this);
this.handleLblChanged = this.handleLblChanged.bind(this);
this.handleYearhanged = this.handleYearChanged.bind(this);
this.handleMakeChanged = this.handleMakeChanged.bind(this);
this.handleModelChanged = this.handleModelChanged.bind(this);
this.handlePlateChanged = this.handlePlateChanged.bind(this);
this.handleODOChanged = this.handleODOChanged.bind(this);
this.handleNotesChanged = this.handleNotesChanged.bind(this);
}
handleBoxChanged (event) {
this.setState({Box: event.target.value})
}
handleVINChanged (event) {
this.setState({VIN: event.target.value})
}
handleLblChanged (event) {
this.setState({Lbl: event.target.value})
}
handleYearChanged (event) {
this.setState({Year: event.target.value})
}
handleMakeChanged (event) {
this.setState({Make: event.target.value})
}
handleModelChanged (event) {
this.setState({Model: event.target.value})
}
handlePlateChanged (event) {
this.setState({Plate: event.target.value})
}
handleODOChanged (event) {
this.setState({ODO: event.target.value})
}
handleNotesChanged (event) {
this.setState({Notes: event.target.value})
}
render () {
return (
<div>
<div className='row'>
<div className='six columns'>
<label>Device #</label>
<input
className='u-full-width'
placeholder='Device #'
type='text'
onChange={this.handleBoxChanged}
value={this.state.Box}
autoFocus
/>
</div>
</div>
<div className='row'>
<div className='six columns'>
<label>VIN</label>
<input
className='u-full-width'
placeholder='VIN'
type='text'
onChange={this.handleVINChanged}
value={this.state.VIN}
/>
</div>
</div>
<div className='row'>
<div className='six columns'>
<label>Label</label>
<input
className='u-full-width'
placeholder='Label'
type='text'
onChange={this.handleLblChanged}
value={this.state.Lbl}
/>
</div>
</div>
<div className='row'>
<div className='six columns'>
<label>Year</label>
<input
className='u-full-width'
placeholder='Year'
type='text'
onChange={this.handleYearChanged}
value={this.state.Year}
/>
</div>
</div>
<div className='row'>
<div className='six columns'>
<label>Make</label>
<input
className='u-full-width'
placeholder='Make'
type='text'
onChange={this.handleMakeChanged}
value={this.state.Make}
/>
</div>
</div>
<div className='row'>
<div className='six columns'>
<label>Model</label>
<input
className='u-full-width'
placeholder='Model'
type='text'
onChange={this.handleModelChanged}
value={this.state.Model}
/>
</div>
</div>
<div className='row'>
<div className='six columns'>
<label>Plate</label>
<input
className='u-full-width'
placeholder='VPlateIN'
type='text'
onChange={this.handlePlateChanged}
value={this.state.Plate}
/>
</div>
</div>
<div className='row'>
<div className='six columns'>
<label>ODO</label>
<input
className='u-full-width'
placeholder='ODO'
type='text'
onChange={this.handleODOChanged}
value={this.state.ODO}
/>
</div>
</div>
<div className='row'>
<div className='six columns'>
<label>Notes</label>
<input
className='u-full-width'
placeholder='Notes'
type='text'
onChange={this.handleNotesChanged}
value={this.state.VIN}
/>
</div>
</div>
<div className='row'>
<div className="col-md-4">
<button onClick={this.addNew} variant="primary">Add a New Device</button>
</div>
</div>
</div>
)
}
}
The main problem is that you're trying to call setState inside StepTwo component but you don't store the steps in component's state there.
You're using a separate .js file to store the steps. This way React doesn't get notified when the steps array is changed and thus won't update (it will at some point, when it's re-rendered for some another reason, but you usually don't want that).
Looking at react-multistep docs, it seems that they don't provide a way to update steps array and don't allow you to pass additional props to form components.
I see two ways for you to fix the problem, considering you don't want to introduce a state management library like redux or mobx:
1. Use React's Context API to pass the state and update function to the form components
2. Create a fork of react-multistep to make it pass the props you pass to MultiStep component down to the form components - the library is pretty small (just 1 small component basically). This way you will be able to use component's state.
Solution with Context API:
import React, { Component } from "react";
import Button from "react-bootstrap/Button";
import Form from "react-bootstrap/Form";
import MultiStep from "react-multistep";
import { Route, NavLink, HashRouter } from "react-router-dom";
import newdevice from "./new-device";
export const StepsContext = React.createContext();
class Home extends Component {
state = {
steps: [
{ name: "Job", component: <StepOne /> },
{ name: "device 1", component: <StepTwo /> }
]
};
addStep = newStep => {
this.setState(prevState => ({
steps: [...prevState.steps, newStep]
}));
};
render() {
const { steps } = this.state;
return (
<StepsContext.Provider value={{ steps, addStep: this.addStep }}>
<div>
<h1>Start a New Job</h1>
<div className="container">
<div>
<MultiStep steps={steps} />
</div>
<div className="container app-footer">
<h6>Press 'Enter' or click on progress bar for next step.</h6>
</div>
</div>
</div>
</StepsContext.Provider>
);
}
}
export default Home;
Then, in your form components:
import { StepsContext } from "./Home";
const AddStepComponent = () => (
<StepsContext.Consumer>
{({ addStep }) => (
<button
onClick={() => addStep({ name: "device 2", component: <StepThree /> })}
>
Add a new step
</button>
)}
</StepsContext.Consumer>
);
I keep getting a message that the item I'm trying to access via props is undefined. Can you please tell me why it's not working?
Here is where the instance where the props are attempting to be passed... I'm specifically talking about the tellus and yourTrial props.
import React from 'react'
import Info from './components/Info'
import Upsell from '../../../general/components/order/components/Upsell'
import FormTwo from '../../../general/components/forms/FormTwo'
import Footer from '../../../cbd-desktop/components/layout/Footer'
export default class Order extends React.Component {
render() {
return (
<div>
<div>
<div style={styles.header}>
<img style={styles.leaf} src="./resources/desktop-img/leaves_top.png" />
<img style={styles.logo} src="./resources/desktop-img/logo_white.png" />
</div>
<div style={styles.wrapper}>
<div style={styles.leftWrapper}>
<Info />
<Upsell styles={upsellStyles} />
</div>
<FormTwo styles={formStyles} tellus="Tell us where to send" yourTrial="YOUR TRIAL BOTTLE"} />
</div>
</div>
<Footer style={styles.footer}/>
</div>
)
}
}
And here is where I am trying to display these values on the child... towards the top in two h2s
import React from 'react'
import { connect } from 'react-redux'
import { stepTwoSubmit, saveBillingData } from
'../../actions/formStepTwoActions'
import { addReceiptProduct } from '../../actions/receiptActions'
import FormTwoInputs from './components/FormTwoInputsComponent.jsx'
import Throbber from '../throbber/Throbber'
import FormWarning from './components/FormWarningComponent.jsx'
import Button from '../../../cbd-desktop/components/layout/Button'
const mapStateToProps = state => ({
state:state,
config:state.config,
downsellProduct:state.downsell.downsellProduct || {},
receiptProducts:state.receipt.receiptProducts || []
})
const mapDispatchToProps = {
stepTwoSubmit,
saveBillingData,
addReceiptProduct
}
#connect(mapStateToProps, mapDispatchToProps)
export default class FormTwo extends React.Component {
constructor(props) {
super(props)
componentWillReceiveProps(nextProps) {
let formTwoResponse = nextProps.state.stepTwo.formTwoResponse
this.checkOrderStatus(formTwoResponse)
}
componentDidMount() {
this.fillMainOrder()
this.calculateViewers()
this.calculateTimer()
}
render() {
let { state, props, inputs, onInputFocus, saveInputVal, styles } = this,
CustomTag = props.labels ? 'label' : 'span',
{ submitting, formWarning } = state,
{ invalidInputID, text, visible } = formWarning
return (
<div style={styles.formWrapper}>
<p style={styles.yellowBanner}>{this.state.viewers} people viewing this product right now</p>
<div style={styles.formInnerWrapper}>
<div style={styles.headerWrapper}>
<h2 style={styles.header}>{this.props.tellus}</h2>
<h2 style={styles.header}>{this.props.yourTrial}</h2>
</div>
<div style={styles.weAccept}>
<p style={styles.weAcceptText}>We Accept:</p>
<img style ={styles.cardImage} src="resources/desktop-img/cards.png" />
</div>
<form onSubmit={this.submit}>
<FormTwoInputs onInputFocus={onInputFocus} saveInputVal={saveInputVal} CustomTag={CustomTag} styles={styles} />
<FormWarning visible={visible} invalidInputID={invalidInputID} text={text} />
<Button style={styles.button} buttonText="RUSH MY TRIAL" />
</form>
</div>
<img src="resources/desktop-img/secure.png" />
<div style={styles.urgencyWrapper}>
<div style={styles.urgencyTextWrapper}>
<span style={styles.redText}>{this.state.viewers} people are viewing this offer right now -</span>
<span style={styles.blueText}>{this.state.counter}</span>
</div>
<p style={styles.blueText}>Claim Your Bottle Now</p>
</div>
<Throbber throbberText='Confirming your shipment...' showThrobber={submitting} />
</div>
)
}
}
I am trying to make a todoList by ReactJS. I want to delete an item by its id, but when I console.log(id), it returns undefined. Here is my code
App.js:
import React, { Component } from 'react';
import './App.css';
import Header from './Components/header';
import InputTodo from './Components/todoInput';
class App extends Component {
constructor(props){
super(props);
this.state={
todos:[
{id:0,text:'Make dinner'},
{id:1,text:'Fold the laundary'},
{id:2,text:'Do homework'}
]
}
}
addHere=(text)=>{
this.setState({
todos:this.state.todos.concat([text])
})
}
removeHere=(id)=>{
console.log(id);
// let arr=this.state.todos;
// let index=arr.findIndex((x)=>x.id===id);
// console.log(index);
}
render() {
return (
<div className='todo-wrapper'>
<Header/>
<InputTodo todoText='Type Here...' addTodo={this.addHere}/>
<div>
{this.state.todos.map((value,key)=>{
return (
<div className='row myList' key={key}>
<p className='col-xs-10'> {value.text}-{value.id} </p>
<button className='btn btn-danger pull-right col-xs-2' onClick={this.removeHere(value.id)}>Delete</button>
</div>
)})}
</div>
</div>
);
}
}
export default App;
The following is InputTodo.js:
import React, {Component} from 'react';
import '../App.css';
export default class InputTodo extends Component{
constructor(props){
super(props);
this.state={
todoInput:{
id:2,
text:''
}
}
}
handleSubmit=(e)=>{
if(this.refs.title.value===''){
alert('You must input something');
}
else{
this.state.todoInput={
id:this.state.todoInput.id+1,
text:this.refs.title.value
};
this.setState(this.state);
this.props.addTodo(this.state.todoInput);
this.refs.title.value='';
}
e.preventDefault();
}
render(){
return(
<form className='input-group' onSubmit={this.handleSubmit}>
<input type='text' ref="title" className='form-control'placeholder={this.props.todoText}/>
<span className='input-group-btn'>
<input type='submit' value='Submit' className='btn btn-primary' />
</span>
</form>
);
}
}
While FuzzyTree's answer will work, a cleaner approach would be extracting the todo item's JSX into its own component. This would have the added benefit of not creating a new function for the button's onClick prop every time App's render function gets called.
The component might look like this:
// TodoItem
class TodoItem extends Component {
handleRemove = () => this.props.onRemove(this.props.id)
render() {
return (
<div className='row myList'>
<p className='col-xs-10'> {this.props.text}-{this.props.id} </p>
<button className='btn btn-danger pull-right col-xs-2' onClick={this.handleRemove}> Delete </button>
</div>
)
}
}
// App render
render() {
return (
<div className='todo-wrapper'>
<Header/>
<InputTodo todoText='Type Here...' addTodo={this.addHere}/>
<div>
{this.state.todos.map(({ id, text }, key) =>
<TodoItem key={key} id={id} text={text} onRemove={this.removeHere} />
)}
</div>
</div>
);
}