3
Hi... I’m studying React and now Redux. I’m trying to make a simple example: Where I have an input and button, I want to enter a value in the input and when clicking the button take the value and add 1.
I managed to make:
- Click on the button to pick up the value and display elsewhere.
- Click on the button to take the initial value and add 1.
But I want to enter the value and click add 1;
I have the following code:
type js.
export const CLICK_BOTAO = 'CLICK_BOTAO',
DIGITAR_VALOR = 'DIGITAR_VALOR';
actions.js
import {
CLICK_BOTAO,
DIGITAR_VALOR
} from './types';
const _valor = (valor) => {
let s =0;
let t =parseFloat( s + valor)
return t;
};
export const clickBotao = value => ({
type: CLICK_BOTAO,
novoValor: _valor(value)
});
export const digitarValor = ev => ({
type: DIGITAR_VALOR,
novoValor: ev.target.value
});
index.js reducers
import {combineReducers} from 'redux';
import clientesReducer from './clientesReducer';
const rootReducer = combineReducers({
click: clientesReducer
})
export default rootReducer;
clientesReducers.js
import {
CLICK_BOTAO,
DIGITAR_VALOR
} from '../actions/types';
const inicial = {
novoValor: 0
}
export default (state = inicial, action) => {
switch(action.type) {
case CLICK_BOTAO:
return {
...state,
novoValor: action.novoValor
};
case DIGITAR_VALOR:
return {
...state,
novoValor: action.novoValor
};
default:
return state;
}
};
Application.js
import React, { Component } from "react";
import { connect } from "react-redux";
import * as actions from "../actions/index";
class Aplicacao extends Component {
constructor(props) {
super(props);
this.state = {
inputValue: 0
};
}
inputChange = event => {
this.setState({
inputValue: this.props.digitarValor(this.props.novoValor)
}, ()=>{
console.log(this.state.inputValue);
});
};
soma = () => {
this.setState({
inputValue: this.state.inputValue + 1
}, () => {
this.props.clickBotao(this.state.inputValue)
console.log(`--- ${this.state.inputValue}`);
}
);
}
render() {
const { novoValor, digitarValor: onChange } = this.props;
return (
<div className="App" style={{ padding: "20px" }}>
<input
onChange={onChange}
type="text"
value={novoValor}
/>
<button onClick={this.soma}>
Clique aqui
</button>
<h1>{novoValor}</h1>
</div>
);
}
}
const mapStateToProps = state => ({
novoValor: state.click.novoValor
});
export default connect(
mapStateToProps,
actions
)(Aplicacao);
What should change to get the result I want?
You want to add the value 1 to the click or add the value of what was typed?
– Daniel Obara
I’m not sure, but try passing the direct value instead of Ev.target.value to your action. Actually if you are going to analyze, if you want to add only the entered value, you don’t even need the DIGITAR_VALOR action. Just let your button click action take the value of Event.target.value inside the same component.
– Daniel Obara
Just clicking I managed to do and already this adding... only it always starts from 0 ai I click 1 - 2 -3 .... I want to enter the value in the input and add example: Type 10 and click there it takes the 10 + 1 = 11 and ai 12 - 13 - 14.
– Alex Passos