jsx

How do I write an if statement to determine whether to display an attribute in jsx

若如初见. 提交于 2019-12-25 06:34:31
问题 This throws an error. Unexpected token for eachOption: <input {eachOption===selectedValue?'checked="checked"':''} type="checkbox" name="checkme"/> 回答1: Replace with checked={eachOption === selectedValue} . That should do the trick. However, make sure you also have the onChange handler attached to the checkbox (as any other input that you would like to use in React), otherwise it might not behave as you'd expect. You should set values for attributes using JSX. By setting the value of checked

React: state not updating on first click

旧街凉风 提交于 2019-12-25 00:18:44
问题 I am working on a shopping cart sample. On each click, I am adding an object of the project to the Cart array. When I click the add cart button the first time, it doesn't update the Cart, but it updates on the second time. Although, when I click the viewCart button in the render's return statement, it shows the accurate number of items in the cart. See my code below: I need to be able to see the accurate number of items in the cart without clicking the viewCart button. The products come from

How to modify React component to use hooks?

扶醉桌前 提交于 2019-12-24 22:42:51
问题 Below is a login component without hooks. This component has two input fields and a submit button. How can we modify this component to use hooks and convert this component into a functional component that can use states? import React from 'react'; import { userService } from '../services/user.services'; class Login extends React.Component { constructor(props) { super(props); this.state = { username: '', password: '', submitted: false, loading: false, error: '' } this.handleChange = this

Component won't render in react

半腔热情 提交于 2019-12-24 19:46:24
问题 Following is my code: import React, { Component } from 'react'; import './App.css'; class buttonGroup extends Component{ render(){ return (<button onClick={this.countClick}>Count it up</button>); } } class App extends Component { constructor(props) { super(props); this.state = { message:"state from constructor", counter: 1 }; this.handleClick = this.handleClick.bind(this); this.countClick = this.countClick.bind(this); } handleClick(e) { this.setState( () => ({message:"state from click handler

Call React Parent component function from outside of the Child Component Class

有些话、适合烂在心里 提交于 2019-12-24 19:40:42
问题 Typically I do the following types of things from a Parent component this.childZ = React.createRef(); // this.handleTabs = this.handleTabs.bind(this); // bind a function //Actual function in the parent class component handleTabs = () => { ...} // I want to call this from Child Component - but outside the class (i have 3rd party javascript etc.. //In my render() <Tab eventKey={19} title="CORE SEC Z."><SectionZ ref={(secZComponent) => {window.secZComponent.handleTabs() = secZComponent}}

map is not a function in jsx

為{幸葍}努か 提交于 2019-12-24 19:18:58
问题 This block of code caused error of map is not a function {data && (data.devices || {}).map((obj, i) => <div>{obj.name}</div> )} I just don't get it, I already did data && to check data is defined else keep the map. And also data.devices || {} to check if the devices property is there or not. I console.log(data.devices) is return undefined but it should fallback to an object right? why is it still breaking? 回答1: There is no native .map to {} , so replace data.devices || {} to data.devices || [

Reactjs console errors: 'Components object is deprectated' & 'ReferenceError: require is not defined'

房东的猫 提交于 2019-12-24 18:40:30
问题 I'm trying to use jsx with classes create a simple hello world program to print 'Hello world' in my browser(firefox). I can get a single page [html with embedded jsx][1] to work. But not when I try to use classes. I am receiving the following in my console output Download the React DevTools for a better development experience: https://fb .me/react-devtools You might need to use a local HTTP server (instead of file://): https://fb .me/react-devtools-faq react-dom.development.js:21347:9

React JSX Component and Await

风流意气都作罢 提交于 2019-12-24 10:38:48
问题 I have an async/await function of which I want to use the result to conditionally render a jsx component in my React native application. This a class method that returns a promise. The method has async declared. The problem is I want to use the result of this to conditionally render some jsx. const hasEdit = await perms.has('/page', 'edit'); return ( <div> { hasEdit && <Button icon='pencil' text='EDIT' onTouchTap={ () => { props.onEdit(); } } /> } </div> ) This returns: A valid React element

insert html tags inside the jsx object for mapping

倖福魔咒の 提交于 2019-12-24 10:04:04
问题 I have a string inside an object with some HTML tags in it, and I want to print the values inside that object using map function. const tableBody = [ { date: '02/08/2019', item: 'Renewed your <strong>current Team Plan</strong>' }, { date: '05/09/2019', item: 'Renewed your current Team Plan 2' }, { date: '15/10/2019', item: 'Renewed and upgraded to the <strong> Business Plan </strong> with 3 new members' } ]; <table> {tableBody.map(tableBody => ( <tr> <td>{tableBody.date}</td> <td>{tableBody

Highlight textbox on edit

送分小仙女□ 提交于 2019-12-24 08:26:59
问题 In continuation to Question , if I have a row that is currently being edited (via state), how do I keep the input textbox highlighted only if edited and stays highlighted even after clicking on next row? My code looks like this: <table className="table-data"> <tbody> <tr> <th>Name</th> <th>Age</th> <th>Class</th> <th>Section</th> </tr> { this.state.students.map((item,key) => { const editField = (value, index) => { // Clone students data before mutation const students = this.state.students.map