简体   繁体   English

当您在本机反应中有多个重复组件时该怎么办

[英]what to do when you have multiple repetitive components in react native

So I have the following react native elements component: 所以我有以下反应本机元素组件:

<CheckBox
   containerStyle={[styles.checkBox, {borderColor: this.getCheckBoxColor(this.state.art)}]}
   textStyle={[styles.text, {color: this.getCheckBoxColor(this.state.art)}]}
   title='Art'
   checkedColor={this.getCheckBoxColor(this.state.art)}
   uncheckedColor={this.getCheckBoxColor(this.state.art)}
   checkedIcon='paint-brush'
   uncheckedIcon='paint-brush'
   size={15}
   checked={this.state.art}
   onPress={() => this.setState({art: !this.state.art})}
/>

Basically, my app will show a series of checkboxes like this asking the user about different topics they enjoys talking about. 基本上,我的应用程序将显示一系列这样的复选框,询问用户他们喜欢谈论的不同主题。 In this case, this checkbox asks the user if they enjoy talking about art. 在这种情况下,此复选框询问用户是否喜欢谈论艺术。 Each topic will therefore have a checkbox and a value in state (in this case, the value in state is this.state.art) and the value in state can either be true or false depending on whether the checkbox is checked or not. 因此,每个主题将具有一个复选框和一个状态值(在这种情况下,状态值是this.state.art),状态值可以为true或false,取决于是否选中了该复选框。 The function getCheckBoxColor takes in the state value and sets the color of the checkbox accordingly (if the state is true, the checkbox turns white, if it's false the checkbox turns gray). 函数getCheckBoxColor接收状态值并相应地设置复选框的颜色(如果状态为true,则复选框变为白色,如果状态为false,则复选框变为灰色)。 The thing is, there are 14 different topics and I don't want to copy and paste this component 14 times and have to change the topic in every single one. 事实是,有14个不同的主题,我不想复制和粘贴此组件14次,而不必在每个主题中都更改主题。 I was thinking about maybe having a reusable checkbox component to use as a child component and pass down the topic-specific data it needs as props (like the title, the checked and unchecked icon, and the value in state) but, since the checkbox needs to alter the state when it is pressed, I'm not sure how to accomplish this because React Native only allows child components to alter the state of the parent component by passing a callback function as a prop to the child, but I don't wanna write 14 different callback functions in my parent component. 我在考虑也许有一个可重用的复选框组件用作子组件,并向下传递它需要作为道具的特定主题数据(例如标题,选中和未选中的图标以及状态值),但是,由于复选框需要在按下状态时更改状态,我不确定如何完成此操作,因为React Native仅允许子组件通过将回调函数作为道具传递给子组件来更改父组件的状态,但我不知道想在父组件中编写14个不同的回调函数。 Is there a better way? 有没有更好的办法?

You could use a generic function in the parent component such as handleCheckboxUpdate = (name) => this.setState({ [name]: !this.state[name] }) . 您可以在父组件中使用通用函数,例如handleCheckboxUpdate = (name) => this.setState({ [name]: !this.state[name] })

Then, the child element can call it with this.props.onCheckboxUpdate(this.props.title.toLowerCase()) . 然后,子元素可以使用this.props.onCheckboxUpdate(this.props.title.toLowerCase())进行调用。

If the child element's title prop won't always match the key in the parent's state, you could add an additional name prop. 如果子元素的标题属性在父项状态中并不总是与键匹配,则可以添加其他name属性。

Define your 14 topics like this. 这样定义您的14个主题。

const topics = [{name: 'art', title: 'Art'}, ...]

Define a common method to render your checkbox. 定义一个通用方法来呈现您的复选框。

renderCheckbox(key, title) {
  return (
    <CheckBox
      containerStyle={[styles.checkBox, {borderColor: this.getCheckBoxColor(this.state[key])}]}
      textStyle={[styles.text, {color: this.getCheckBoxColor(this.state[key])}]}
      title=title
      checkedColor={this.getCheckBoxColor(this.state[key])}
      uncheckedColor={this.getCheckBoxColor(this.state[key])}
      checkedIcon='paint-brush'
      uncheckedIcon='paint-brush'
      size={15}
      checked={this.state[key]}
      onPress={() => this.setState({ [key]: !this.state[key] })}
    />
  )
}

Now loop through your topics and render all 14 checkboxes. 现在遍历您的主题并呈现所有14个复选框。

renderAllTopics(topics) {
  return(
    {topics.map((row, index) => (
      this.renderCheckbox(row.name, row.title)
    ))}
  )
}

In case you want to use only for specific topics, you can render like this: 如果只想用于特定主题,则可以这样渲染:

this.renderCheckbox(topicName, topicTitle)

Hope this helped. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM