我想在React中显示一个小吃店

时间:2018-08-01 10:03:24

标签: javascript reactjs

这是单击按钮时要调用的功能。

public static void main(String[] args) {
    List<Company> companies = Arrays.asList(
            new Company(Arrays.asList(new Person("Jon Skeet"), new Person("Linus Torvalds"))),
            new Company(Arrays.asList(new Person("Dennis Ritchie"), new Person("Bjarne Stroustrup"))),
            new Company(Arrays.asList(new Person("James Gosling"), new Person("Patrick Naughton")))
    );

    List<String> persons = companies.stream()
            .flatMap(company -> company.getPersons().stream())
            .map(Person::getName)
            .collect(Collectors.toList());

    System.out.println(persons);
}

这是我的onClick handleClick() { var x = document.getElementById("snackbar"); x.className = "show"; setTimeout(function () { x.className = x .className .replace("show", ""); }, 3000); return ( <div id="snackbar">DONE</div> ) }

它向我显示了一个错误,“未捕获(承诺)TypeError:无法将属性'className'设置为null” 我究竟做错了什么。感谢所有帮助。

1 个答案:

答案 0 :(得分:1)

  

TypeError:无法将属性'className'设置为null”

意味着您试图访问null变量中的classname属性。

如果var x = document.getElementById("snackbar");没有结果,它将返回null,并且x等于null。 在var x = document.getElementById("snackbar");之后添加console.log(x),以便您测试它是否来自此处。

可能是由于以下原因引起的:

Invalid HTML syntax (some tag is not closed or similar error)
Duplicate IDs - there are two HTML DOM elements with the same ID
Maybe element you are trying to get by ID is created dynamically (loaded by ajax or created by script)?