简体   繁体   English

通过父组件的setState更改道具时,子组件中的数据列表选项不会更新

[英]Datalist options in child component won't update when props are changed through parent component's setState

The parent component updates its state with retrieved list from database using setState .The state is passed to its child component via props. 父组件使用setState从数据库中检索列表更新其状态。状态通过setState传递给其子组件。 When the parent's state is updated, the datalist in the child component is still empty list. 当父状态更新时,子组件中的数据列表仍为空列表。

Parent component: 父组件:

export default class Major extends Component {
        constructor(props) {
            super(props)
            this.state = {
                uni: []
            }
        }
        componentDidMount() {

            axios.get('http://localhost:3001/uni').then((res) => {
                this.setState({uni: res.data})
            }).catch((e) => {
                console.log('something wrong occured: ' + e)
            })
        }
        render(){
            return (
                <div>
                    <Datalsit_ list={this.state.uni} list_id='abc' />
                </div>

            )
        }
    }

Childe component: 子组件:

export default function Datalist({list_id, list}){
    return(
        <div>
            <input type="text" list={list_id} />
            <datalist id={list_id} >
            { list.map((each) => {
                <option value={each} />
            })}
            </datalist>
        </div>
    )
}

What part goes wrong? 哪一部分出了问题?

There are typos in your code (in your parent Datalist is spelled as Datalsit_ )... Having said that... Here is your parent component: 您的代码中有错别字(在您的父数据列表中拼写为Datalsit_)...话虽如此...这是您的父组件:

export default class Major extends Component {
  state = { uni: [] };

  componentDidMount() {
    axios
      .get('http://localhost:3001/uni')
      .then(res => {
        this.setState({ uni: res.data });
      })
      .catch(e => {
        console.log('something wrong occured: ' + e);
      });
  }
  render() {
    return (
      <div>
        <DataList list={this.state.uni} list_id="abc" />
      </div>
    );
  }
}

I have no idea why you're passing the list_id prop as the string 'abc' but that's beside the point... 我不知道为什么你要把list_id prop as the string 'abc'传递list_id prop as the string 'abc'但这list_id prop as the string 'abc' ...

Here is your Child component: 这是您的子组件:

export default function Datalist({ list_id, list }) {
  return (
    <div>
      <input type="text" list={list_id} />
      <datalist id={list_id}>
        {list.map((each, i) => {
          return <option key={i} value={each} />;
        })}
      </datalist>
    </div>
  );
}

Note the return before the <option /> ... And I'm assuming the value is probably an object so in the datalist you're going to see [object, Object] It needs to be value={each.something} (I have no idea what as I can't see the structure of the data coming from the API at http://localhost:3001/uni ) 请注意<option />之前的return值...而且我假设该值可能是一个对象,因此在数据列表中,您将看到[object, Object]它必须是value={each.something} (我不知道怎么办,因为我看不到来自http://localhost:3001/uni的API的数据结构

Again, I have no idea why you would put a list prop on your input ... but in any case, this should fix it... 再说一次,我不知道为什么要list prop on your input放一个list prop on your input ……但是无论如何,这应该解决它……

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

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