I have to access the value of a button, because it holds the ID which is needed for further progress.
I first used a normal button with some bootstrap styles and everything worked fine.
<button
value={row.vacationRequestID}
className="btn btn-warning"
onClick={myRef.handleDeclineClick.bind(myRef)}>No
</button>
handleDeclineClick(e, value) {
console.log("decline");
console.log(e.target.value) //ID: 120
// this.props.declineClick(e);
//dispatch(requestStatusUpdate(e.target.value, declined, reason))
}
Now after using the material-ui, I can´t access the value anymore.
<IconButton
iconClassName="material-icons"
tooltip="Ablehnen"
value={row.vacationRequestID}
ref={"dd"}
onClick={myRef.handleDeclineClick.bind(myRef)}
>
thumb_down
</IconButton>
I have tried to access it via ref, but not even this is working anymore. Can someone explain me why? The Documentation doesn´t say anything about value.
Use currentTarget instead of target
handleDeclineClick(e, value) {
console.log("decline");
console.log(e.currentTarget.value);
}
Original Answer
MaterialUI doesn't use any value prop. So when you set the value prop to the IconButton, it means essentially nothing.
If you'd want to pass any value to the onClick call back function, bind the value to the function. So when it is called, you'll get it as the first argument.
myRef.handleDeclineClick.bind(myRef, row.vacationRequestID)
Related
I am using react bootstrap AsyncTypeHead. I type keywords and it suggests values from the backend, then I click one of the suggestions and try to get the value selected, but it seems event does not exist. It throws all kind of errors. What could be the issue? I am trying to do it through onChange attribute with event.target.value:
<AsyncTypeahead
filterBy={filterBy}
id="async-example"
isLoading={isLoading}
labelKey="login"
minLength={3}
onSearch={handleSearch}
onChange={ (event) => setCitiesSend([...citiesSend, event.target.value])}
If instead event.target.value I put alert('test') it throws alert multiple times. It seems to be changing on each typed letter.
Full example: React Bootstrap Typeahead - Asynchronous Searching
The onChange function returns a value.
So, your code will be.
<AsyncTypeahead
filterBy={filterBy}
id="async-example"
isLoading={isLoading}
labelKey="login"
minLength={3}
onSearch={handleSearch}
onChange={ (value) => setCitiesSend((previous) =>[...previous, ...value])}
/>
In my render method i render some cards that all have button and when i click on the button i want to disabled them.
Those buttons are checking if the value of the array is true and if it is it's disabling the button but this is only working when i refresh the page and i want the button to be disabled directly on click
Here is my try
//this is checking from my database if it include the id of the pokemon that i want to get
const check = pokemon.id
const newPoke = getPokemon.includes(check);
// Here is the button that is getting disabled only if newPoke return true
<Button isDisabled={newPoke}/>
Everything work correctly when i refresh the page the buttons that i clicked previously get disabled but not onClick directly.
I think after i click i have to re-check for newPoke but i'm not sure how to include it in the button
You are using props to pass variable from parent to children(Button).
reactjs documentation states react props are read-only
If you are planing to change the values of newPoke then you should rethink your structure and maybe its best to use states in your example
For example here
<Button isDisabled="newPoke"></Button will only be checked at initialization stage since its a react prop. but if you used state which gets updated with every new action you can have a true one-way binding and your ui would reflect data changes quickly
store the newPoke in state and change it's value when the button is clicked. and use that value in button isDisabled prop. try something like this
state={
isDisabled: false;
}
handleClick=(isDisabled)=>{
this.setState({isDisabled});
}
//this is checking from my database if it include the id of the pokemon that i want to get
const check = pokemon.id
const newPoke = getPokemon.includes(check);
// Here is the button that is getting disabled only if newPoke return true
return(
<Button isDisabled={this.state.isDisabled} onClick={()=>this.handleClick(newPoke )}/>
);
i think if you want get value from database you can use componentDidMount().
and your value you can save while in state. and then in function handleClick you can change this state
for example :
handleClick = () => {
const value = this.state.valueFromDataBase
this setState({valueFromDataBase: false})
}
and in render you declare this value state
render(){
return(
<Button defaultValue={this.state.valueFromDataBase} onCLick={this.handleClick} />
)
}
please correct my answer if I am wrong in responding to your question
So the image above is my code inside a comp.js (Vue component) when the user clicks 'Update' button, it will grab the updated input and push it to firestore.
I put v-on:emit on every single input and I tried to reference it on the 'Update' button.
This is my modal inside the HTML file, I tried to connect the function from comp.js to function.js.
However, the methods above didn't work and it gave me the error below.
I'm not really sure how to grab the value from each input and connect
UPDATED CODE BASED ON ITTUS'S SUGGESTION:
So based on the Ittuss suggestion below, I tried to pass over to items through the emit inside the Update button, I did this because I want to pass all the input values above the button to update the Firestore database.
I changed the cuisedit to this:
And I updated the function in the .js to this:
However, I received an error saying that I didn't reference correctly.
Do you know why it isn't grabbing the value emitted from the button?
In your cuiseedit component, $emit should contain only 1 additional parameter:
<button #click="$emit('some-change', obj2.id)"></button>
and in component:
<transition name="fade">
<cuisedit v-if="showModal" :obj2="cuisinemodal" #some-change="updateData">
</cuisedit>
</transition>
updateData: function (id) {
firestore.collection("koreanbap-cuisines").doc(id).update()....
}
Note
If you want to pass more data, you should wrap them to an object
<button #click="$emit('some-change', {id: obj2.id, event: $event})"></button>
I am creating a custom button component in react. I want to pass a prop to that button, based on the value of which button gets enabled or disabled.
My problem is
- The mere presence of the disabled property disables the element, so I cannot set its value as "false". Even the following code is disabling the element
<input type="button" id="myBtn" value="Submit" disabled="" />
I need to either remove the attribute completely or set its property via javascript.
document.getElementById("myBtn").disabled = true;
My custom button component is -
import React from 'react';
const CustomButton = ({ whenClicked, classNames, buttonText, isDisabled }) =>
(
<button
onClick = {whenClicked}
className = {`btn ${classNames}`}
type = "button"
disabled = {isDisabled}
>
{buttonText}
</button>
);
export default CustomButton;
isDisabled is a boolean value.
One more thing, I lost the default submit behavior of button while using custom button. Now I always need to pass a click handler. Is there any way to achieve the same behavior again?
What you currently have should work perfectly fine. Be careful that when you use CustomButton you don't send in the value for disabled as a string. That will make it disabled regardless of what you pass in. Instead, you need to pass in a boolean, that's in JSX syntax.
Here's a sample usage where you would put it inside of a render function of a component that uses the button:
...
render() {
<div>
...
<CustomButton
whenClicked={() => console.log('I just got clicked')}
buttonText="Some Button"
isDisabled={false}
/>
...
</div>
}
...
Basically, instead of false you could pass in something else. You could pass in a boolean value that's stored on the props of the container that holds the CustomButton.
You could say isDisabled={this.props.disableInnerButton} and it would work as you would expect. Changing the value of the boolean will disable or enable the button.
Below you can find a relevant answer I gave recently on a very similar topic:
Statement to add or not an attribute in JSX
At first it looks quite complicated to handle but, if we look at the problem I think we can achive it using css only by adding a class(selector) to the element
.disabled { pointer-events: none;}
or conditionally styling:
<button style={{pointerEvents: notValid ? "none" : "initial"}}> Submit </button>
when using the click bind in knockout, how does knockout know to pass the correct parameter to the method its bound to?
<div id="test" data-bind="click: runTest"/>
</div>
self.runTest = function (coolParameter){
doSomethingCool();
}
When calling your handler, Knockout will supply the current model
value as the first parameter. This is particularly useful if you’re
rendering some UI for each item in a collection, and you need to know
which item’s UI was clicked.
from the documentation
There also is some discussion in the docs about how to pass more parameters by adding a wrapping function
<button data-bind="click: function(data, event) {
myFunction('param1', 'param2', data, event)
}">
Click me
</button>
knockout understands which value to pass from context. it's the current model object. e.g if you're in a foreach knockout passes the current item.