score:0
TextField
is a wrapper component.
You can pass the ref
to the native input
like this:
import React, { Component, createRef } from 'react';
import TextField from '@material-ui/core/TextField';
export default class App extends Component {
ref = createRef();
render() {
return (
<div className="App">
<TextField inputProps={{ ref: this.ref }} />
</div>
);
}
}
score:1
You must initiate state values inside the constructor.
Also change this line as inputRef={this.newActivity}
instead of inputRef={(el)=>this.newActivity =el}
. Because you already create ref using createRef no need to create again.
class Activity extends Component {
constructor(props) {
super(props);
this.state = {
activity: this.props.data.name
}
this.callAPI = this.callAPI.bind(this);
this.newActivity = React.createRef();
}
callAPI() {
fetch("http://localhost:5000/activities", {
method: 'POST',
body: JSON.stringify({
newName: this.newActivity.current,
oldName: this.state.activity
}),
})
.then(function (response) {
return response.json()
});
}
render() {
return (
<React.Fragment>
<Grid justify="center" container spacing={(2, 10)} aligncontent="center">
<Grid item xs={8} >
<Paper>
{/*Trying to get the input here so that I can put it into my POST request*/}
<TextField inputRef={this.newActivity} type="activity" id="standard-basic" label="Standard" defaultValue={this.state.activity} />
</Paper>
</Grid>
<Grid item xs={2}>
<Button onClick={this.callAPI} variant="contained" startIcon={<UpdateIcon />} style={buttonStyle} >Uppdatera</Button>
</Grid>
<Grid item xs={2}>
<Button variant="contained" startIcon={<DeleteIcon />} style={buttonStyle} >Ta Bort</Button>
</Grid>
</Grid>
</React.Fragment>
);
}
Source: stackoverflow.com
Related Query
- Unable to get the input from a textfield in React
- get the value from a text input after it is updated in react function
- is it possible to get multiple input from the input field which is created dynamically in react js?
- How can we get the input text from multiple dynamic textareas in react hook?
- React - How can I get the values from my multiple input and send them to my input?
- How to pass input from React Form to Axios get request and display the request result?
- Node application interactively get user input from react front end by pausing the current execution
- React : how to get input value from one component to make the ajax call in another component?
- Unable to get the selected value from the React fluent UI dropdown
- Can not get the value from Input React (react-rails gem)
- React + Redux - Input onChange is very slow when typing in when the input have a value from the state
- How to get the data from React Context Consumer outside the render
- How can I get a variable from the path in react router?
- Unable to get the name of the input when using getByRole while testing with testing-library
- React Typescript: Get files from file input
- How to get form data from input fields in React
- How do I get my React TextField to open the number pad when inputting on a mobile device?
- React what's the right way to get a parent props from its children
- Get value from checked input in React
- How can I get the value from an animated or interpolated value in React Native?
- How to get input value of TextField from Material UI without using onChange event?
- React - Get the value of an input by clicking on a button
- How to get JWT cookies in our react application, how to check the user is login or not I am unable to find how to handle my react application session
- How do I pass in the variant property of the material-ui TextField from a wrapping React component
- Unable to center the `Dialog/Modal` from `@headlessui/react` that uses React Portal with Tailwind CSS?
- How to get the selected value from a radio button group in React where there are multiple groups?
- How to get input value of TextField from Material UI?
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- React JS React Query's useMutation: Unable to retrieve the response from the server within the onError callback
- Get value from input and use on the button
More Query from same tag
- firebase__webpack_imported_module_2__.auth.createuserwithemailandpassword is not a function.?
- useEffect Running indefinitely
- How should I implement a watch method in react hooks
- React hook Search on button click not working
- How to use jinja2 server side rendering alongside react without violating inline-script CSP
- How do I import a Mobx state tree and reference its values in a Typescript file without a component?
- Confusion over javascript .map() is able to modify the original array instead of assigning a transformation of the array
- Different wrap text between horizontal and vertical
- Loading p5js of react-p5 npm package to NextJS app shows "ReferenceError: window is not defined"
- Fetch on react / redux not work properly
- Using React state, how can I remove markers from a Google map before adding new ones?
- How to change cursor position with draft.js?
- React Typescripted Higher order component loses generic prop type of the passed component
- How using comma character like enter in input at React
- React not rendering with correct state value after using this.setState
- Spreading Props TypeScript with React and Styled Components
- Wait for axios response before execute code in useEffect
- React lifecycle events cancel GSAP animation
- How to add async attribute to script generated by react scripts (at the npm run build stage on the create-react-app template)
- How to set a null value or equivalent to a field?
- Regex Get Numbers in Dash or Underscore
- React.js: Cancel button is cancelling editing but leaving the state with the changed value after editing was started
- How do I export these function stored in const?
- Not getting variable in style attribute
- Issues adding additional entry point to webpack config (create react app)
- Dynamically setState() with key nested state
- Why doesnt my dispatch for axios POST work?
- Why are react-table pagination and filter reset by changing its data?
- How to call a function with different values without recreating an new arrow function on every render?
- Difference between variable in and out constructor function