“setState” Respostas de código

Reactor de componente de classe nativa

import React from 'react';  
import { View, TextInput } from "react-native";

class App extends React.Component {  
  constructor(props){  
    super(props);  
    this.state = {  
         name: "" 
      }  
    this.handleChange = this.handleChange.bind(this);  
  }
  handleChange(text){
    this.setState({ name: text })
    console.log(this.props);  
  }  
  render() {  
    return (  
    <View>
      <TextInput 
      	value={this.state.name} 
  		onChangeText={(text) =>this.handleChange(text)}
      />
    </View>  
  }  
}  
export default App;  
Open Owl

Como definir o estado na função React

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "count" 
  const [count, setCount] = useState(0);
  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
Spotless Spider

setState

this.setState((state, props) => ({
  counter: state.counter + props.increment
}));
Thoughtless Tarantula

setState

this.setState((state, props) => ({
  counter: state.counter + props.increment
}));
Thoughtless Tarantula

setState

this.setState((state, props) => ({
  counter: state.counter + props.increment
}));
Thoughtless Tarantula

setState

this.setState((state, props) => ({
  counter: state.counter + props.increment
}));
Thoughtless Tarantula

Procure respostas de código populares por idioma

Procurar outros idiomas de código