Is there any way to make a component hear a React Native function?

1

I need to bind to a function that returns me if an item is checked or not, however the function that returns this is only executed once, is there a way to bind to this function?

<List dataArray={this.state.talhoes}
        renderRow={(talhao) =>
      <TouchableOpacity onPress={() => this.select(talhao)}>
        <Card>
          <CardItem>
            <Thumbnail square size={40} source={{ uri: talhao.public_url_screenshot }} />
            <Text style={{ paddingLeft: 10 }} > {talhao.name}  </Text>
            <Right>
              <CheckBox checked={this.checkSelected(talhao)} /> //Aqui o problema
            </Right>
          </CardItem>
        </Card>
      </TouchableOpacity>
    }>
  </List>
    
asked by anonymous 14.10.2017 / 21:44

1 answer

0

You must assign a value from the state of your component or a property that is updated externally, because you need the render method of your component to run for the change to appear on the screen.

An example of how to solve this in a simple way:

// Seu metodo checkSelected, pode alterar o state em vez de retornar o valor a ser definido no <CheckBox/>
checkSelected (value) {
  // exemplo hipotético ok?
  const checked = value !== undefined
  this.setState({ checked })
}

// Quando o state é alterado, o método render é executado e o valor de checked será atribuido corretamente de acordo com o estado atual
<CheckBox checked={this.state.checked)} />
    
15.10.2017 / 21:27