如何将道具从父母传给孩子到孩子?

问题描述 投票:0回答:2

我想把App.js(父)的道具传递给CommentItem.js(子)到Button.js(子的子),但是在Button,js(子的子)的组件中道具是空的。

  1. App.js(父)到CommentItem.js(孩子)

我将mainuser: this.state.head推送到FlatList,并且renderItem={({ item }) => <CommentItem {...item}传递了props(mainuser)

并且,CommentItem.js(孩子)通过以下代码收到mainuser

const {
    head,
    text,
    created,
    mainuser,
    subuser,
  } = 
  1. CommentItem.js(child)到Button.js(孩子的孩子)

我认为道具被传递给Button by,而Button(孩子的孩子)通过下面的代码收到了道具。

const {
      mainuser,
    } = this.props;

但是,在Button.js(孩子的孩子)中道具id为空。

#我的代码有问题吗?你能给点建议吗?


App.js(父)

export default class App extends Component<{}> {

  constructor(props) {
    super(props);

    this.state = {
      head: [],
      list: [],
    };
  }

  _onPress = (text) => {
    const list = [].concat(this.state.list);

   list.push({
      key: Date.now(),
      text: text,
      done: false,
      mainuser: this.state.head,
    });

    this.setState({
      list,
    });
  }

  render() {
    const {
      head,
      list,
    } = this.state;

    var data = [["User1", "User2", "User3"],];

    return (
      <View style={styles.container}>
        <View style={styles.dropdownHeader}>
        <View style={styles.dropdown}>
          <DropdownMenu
            style={{flex: 1}}
            bgColor={'white'}
            tintColor={'#666666'}
            activityTintColor={'green'}
            handler={(selection, row) => this.setState({head: data[selection][row]})}
            data={data}
          >
          </DropdownMenu>
        </View>
        </View>
      <Text>To Do</Text>
            <View style={styles.postinput}>
              <CommentInput onPress={this._onPress} />
            </View>
          </View>
          <View style={styles.CommentListContainer}>
            <FlatList
              /*inverted*/
              style={styles.CommentList}
              data={list}
              renderItem={({ item }) => <CommentItem {...item} /> }
            />
          </View>
    );
  }
}

CommentItem.js(子)

const CommentItem = (props) => {
  const {
    head,
    text,
    created,
    mainuser,
    subuser,
  } = props;


  return (
    <View style={styles.container}>
      <View style={styles.left}>
        <Text style={styles.text}>{text}</Text>
      </View>
      <View style={styles.function}>
        <Button {...mainuser} />
      </View>
    </View>
  );
}

Button.js(小孩的孩子)

export default class ApplauseButton extends Component {

  constructor(props) {
    super(props);
    this.state = {

    };
  }

  render() {
    const {
      mainuser,
    } = this.props;

    return (
      <View style={styles.container}>
        <Text>{mainuser}</Text>
      </View>
    );
  }
}
javascript reactjs react-native react-redux
2个回答
0
投票

根据你的代码,似乎主要用户是数组,

this.state = {
  head: [],//array
  list: [],
};

....
list.push({
      key: Date.now(),
      text: text,
      done: false,
      mainuser: this.state.head,// main user should be array
});

所以你需要在<Button />中给出道具名称

像这样

<Button mainuser={mainuser} />


3
投票

如果你想访问mainuser作为道具,那么你必须将其作为<Button mainuser={mainuser} />传递。

就目前而言,你是spreading mainuser的内容。

© www.soinside.com 2019 - 2024. All rights reserved.