Reactjs 将日期和时间从一个组件解析到另一个组件
我创建了一个组件。它需要返回当前日期和时间Reactjs 将日期和时间从一个组件解析到另一个组件,reactjs,Reactjs,我创建了一个组件。它需要返回当前日期和时间 import * as React from 'react'; class Clock extends React.Component { constructor(props) { super(props); this.state = { date: new Date() } } render() { return (this.date);
import * as React from 'react';
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date()
}
}
render() {
return (this.date);
}
}
我需要在component Home中访问此数据,并使用Home返回的值设置其时间状态
如果我的主页组件如下所示
export class Home extends Component {
displayName = Home.name
state = {
time: null
};
handleTime = (timeValue) =>
this.setState(currentState => ({
time: timeValue
}));
render() {
return (
<div>
<div>
<Clock time="handleTime"/>
</div>
</div>
);
}
}
导出类主扩展组件{
displayName=Home.name
状态={
时间:空
};
handleTime=(时间值)=>
this.setState(currentState=>({
时间:时间值
}));
render(){
返回(
);
}
}
我犯了什么错误?以下代码将在构建时钟组件时设置家庭组件上的时间。但我不确定这是你想要的
import * as React from 'react';
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date()
}
}
componentDidMount() {
this.props.handleTime(this.state.date);
}
render() {
return <View>{this.state.date.toString()}</View>;
}
}
import*as React from'React';
类时钟扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
日期:新日期()
}
}
componentDidMount(){
this.props.handleTime(this.state.date);
}
render(){
返回{this.state.date.toString()};
}
}
您的主组件没有以正确的方式引用handleTime函数(缺少花括号和访问类成员的“this”关键字):
导出类主扩展组件{
displayName=Home.name
状态={
时间:空
};
handleTime=(时间值)=>
this.setState(currentState=>({
时间:时间值
})
);
render(){
返回(
);
}
}
以下代码将在构建时钟组件的那一刻设置家庭组件上的时间。但我不确定这是你想要的
import * as React from 'react';
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date()
}
}
componentDidMount() {
this.props.handleTime(this.state.date);
}
render() {
return <View>{this.state.date.toString()}</View>;
}
}
import*as React from'React';
类时钟扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
日期:新日期()
}
}
componentDidMount(){
this.props.handleTime(this.state.date);
}
render(){
返回{this.state.date.toString()};
}
}
您的主组件没有以正确的方式引用handleTime函数(缺少花括号和访问类成员的“this”关键字):
导出类主扩展组件{
displayName=Home.name
状态={
时间:空
};
handleTime=(时间值)=>
this.setState(currentState=>({
时间:时间值
})
);
render(){
返回(
);
}
}
handleTime
传递到Clock
组件:
<Clock time={this.handleTime} />
handleTime
传递到Clock
组件:
<Clock time={this.handleTime} />
像这样编辑你的家
import React, { Component } from "react";
import Clock from "./Clock";
class Home extends Component {
constructor(props) {
super(props);
this.state = {
time: null
};
}
displayName = Home.name;
handleTime = timeValue => {
this.setState({
time: "timeTest"
});
};
render() {
return (
<div>
<div>
<Clock time={this.state.time} />
</div>
<button onClick={this.handleTime.bind(this)}>Change time</button>
</div>
);
}
}
export default Home;
import React,{Component}来自“React”;
从“/Clock”导入时钟;
类Home扩展组件{
建造师(道具){
超级(道具);
此.state={
时间:空
};
}
displayName=Home.name;
handleTime=timeValue=>{
这是我的国家({
时间:“时间测试”
});
};
render(){
返回(
换乘时间
);
}
}
导出默认主页;
你的时钟是这样的:
import React from "react";
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date(),
time: props.time
};
}
render() {
console.log(this.props.time);
return (
<div>
<div>{this.state.date.toString()}</div>
<div>{this.props.time}</div>
</div>
);
}
}
export default Clock;
从“React”导入React;
类时钟扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
日期:新日期(),
时间:道具
};
}
render(){
console.log(this.props.time);
返回(
{this.state.date.toString()}
{this.props.time}
);
}
}
输出默认时钟;
像这样编辑你的家
import React, { Component } from "react";
import Clock from "./Clock";
class Home extends Component {
constructor(props) {
super(props);
this.state = {
time: null
};
}
displayName = Home.name;
handleTime = timeValue => {
this.setState({
time: "timeTest"
});
};
render() {
return (
<div>
<div>
<Clock time={this.state.time} />
</div>
<button onClick={this.handleTime.bind(this)}>Change time</button>
</div>
);
}
}
export default Home;
import React,{Component}来自“React”;
从“/Clock”导入时钟;
类Home扩展组件{
建造师(道具){
超级(道具);
此.state={
时间:空
};
}
displayName=Home.name;
handleTime=timeValue=>{
这是我的国家({
时间:“时间测试”
});
};
render(){
返回(
换乘时间
);
}
}
导出默认主页;
你的时钟是这样的:
import React from "react";
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date(),
time: props.time
};
}
render() {
console.log(this.props.time);
return (
<div>
<div>{this.state.date.toString()}</div>
<div>{this.props.time}</div>
</div>
);
}
}
export default Clock;
从“React”导入React;
类时钟扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
日期:新日期(),
时间:道具
};
}
render(){
console.log(this.props.time);
返回(
{this.state.date.toString()}
{this.props.time}
);
}
}
输出默认时钟;
您好,谢谢您的回复。返回({this.date});给我一个错误哦,对不起,应该是return({this.state.date});同样的语法错误。这样说是一个保留关键字。/src/components/Clock.js语法错误:C:/Users/src/components/Clock.js:这是一个保留字(14:18),我对主要答案做了一些更改。我想现在我答对了,请再试一次:)嗨,谢谢你的回复。返回({this.date});给我一个错误哦,对不起,应该是return({this.state.date});同样的语法错误。这样说是一个保留关键字。/src/components/Clock.js语法错误:C:/Users/src/components/Clock.js:这是一个保留字(14:18),我对主要答案做了一些更改。我想现在我猜对了,请再试一次:)嗨,谢谢。但得到的错误是,对象作为React子对象无效(发现时间:2018年10月12日星期五05:57:06 GMT+0530(标准时间))。如果要呈现子对象集合,请改用数组。in div(在Clock.js:15)in div(在Clock.js:15)in Clock(在Home.js:37)in div(在Home.js:36)in div(在Home.js:32)in div(在Route中创建)in Route(在App.js:12)in div(在App.js:11)in App(在index.js:16)in Router(在BrowserRouter中创建)in BrowserRouter(在index.js:15)打印状态日期:this.state.date.toString()不是this.state.dateA litt