score:1
without diving too deep into context and binding this
in javascript, the problem is in your onclick
handler. there are a few ways to solve it, but the gist is that you need to have a function that calls your update/submit function when handling events on dom elements.
the idea is that you're binding a function to handle the event, which will then call your function when the event happens.
one way is to create an inline anonymous function:
onclick={() => submitcomment(props.post, state.comment, state.comment)}
i personally prefer to keep this logic outside of the component body, so i'd instead move it up near the rest of your business logic:
// ev is the click event
const submitcomment = (ev) => {
// you can read from state & props here
firebase.firestore().collection('blogs').doc(props.post).update({
comments: firebase.firestore.fieldvalue.arrayunion({
name: state.name,
date: new date().tostring(),
text: state.comment
})
})
}
// later...
<button onclick={ submitcomment }>submit</button>
totally unrelated to the click issue, but also in your code you call:
const togglelike = () => setstate({ ...state, liked: !state.liked })
i just wanted to point out that you can use the current state in your setstate
(for things like toggling likes as you are), but you should read from the previous state to ensure that you're not getting a bad value. the reason for this is that state changes are batched, not immediately fired. so you want to make sure you're reading the previous value before it's updated (in case anything else is updating it):
const togglelike = () => {
setstate(prevstate => { ...prevstate, liked: !prevstate.liked })
}
score:0
can you try using an anonymous callback on your button like so:
onclick={() => submitcomment(props.post, state.comment, state.comment)}
Source: stackoverflow.com
Related Query
- NextJS Button keeps clicking itself by React's useState
- useState counter flickers after clicking the button
- Server-side render a modal in React upon clicking a button for NextJS
- VSCode keeps asking to import React on NextJS and React 17 apps
- React generated button on my form keeps refreshing the page
- useState always is default value in itself
- Display a component on clicking a button in React
- Material UI ClickAwayListener close when clicking itself
- How to make the row editable when clicking on an "Edit" button on a cell with react-table
- Download file by clicking a button in ReactJS
- React JS page keeps refreshing when using the back button
- How to make clicking a menu button toggle show/hide of a menu sidebar component
- How to disable a button in React Native after clicking it
- How to detect,launch and connect to metamask mobile app by clicking on a button on the webapp like on opensea?
- How can I get innerHTML when clicking a button in React js?
- NextJS process.env. variables undefined on button click fetch data
- React - Get the value of an input by clicking on a button
- electron-react-boilerplate :sub window on clicking a button
- Make a button inside an element clickable while the element itself is also clickable
- how to update dropdown list by clicking the update button in react
- react js can't update button status using useState
- unexpected behavior when using Tailwind, and MUI in NextJS Project (White Button error)
- Clicking on button is not adding inline style using draft.js
- Close react button dropdown menu on clicking or hovering outside of menu area
- Re-rendering react component after clicking Like button (with Redux)
- reactjs clear date input after clicking clear button
- How can I print multiiple text areas when clicking on a button in reactjs
- Hide all div and show one div on clicking multiple button
- Dilemma about React Hooks and AJAX call triggered by user by clicking on a button
- React - change button color after clicking
More Query from same tag
- Firebase/React: Get Document ID before creation
- Material ui update select style globally with theme
- Set the index from given array of object having another array of object
- Webpack-dev-server how to remove default routing for react app?
- Reactjs img tag not rendering the string in src:?
- Where can I find documentation on why `this` context is undefined when assigning object method's as DOM event handlers in React?
- What does registerServiceWorker do in React JS?
- React: Array of refs or array in ref.current
- How can I add styles to all the children of an element
- Is there a way to toggle class on React Button but un toggle other buttons so the one clicked is active?
- Is there a way to move style object in PaperProps to makeStyles in material UI?
- How to connect Reused Reducers Logic into React components?
- How do you create an onKeyPress event and pass in parameters to the event handler function?
- How to use a boolean to control other properties as required in typescript?
- Login to chrome extension via website with AWS amplify
- why do I get this error: 'postcss' is not recognized as an internal or external command, operable program or batch file, when I use tailwindcss
- Error Exporting a component without importing
- Fluent UI Web: How to change border radius of TextField?
- Fetched data from external api,how to loop through the array and display values
- Not understanding why my element.url property can t be read by require function
- Hide transparent image from download while using React Konva
- How to get return data from another file outside of a react component?
- How would you implement pinch-zoom in react-native?
- Min and Max Date in Material UI DatePicker in React
- Ant Design: How to prevent expansion when clicked on the Collapse header, but allow on a button?
- Sort number in antd Table
- Warning: An update to Formik inside a test was not wrapped in act(...)
- checking to see if item is "clickable" in HyperSpec
- Exception from Tracker recompute function ( Invariant Violation )
- Socket.emit doesn't trigger for all clients