te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to pass state from one component to another in React js? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass state from one component to another in React js? - Stack Overflow

programmeradmin4浏览0评论

I have ponent like below it have 4 state values

class ComposedTextField extends React.Component {
  state = {
    name: '',
    title: '',
    email: '',
    experience: ''
  };

  handleChange = event => {
    this.setState({ 
          [event.target.name]:event.target.value,
          [event.target.title]:event.target.value,
          [event.target.email]:event.target.value,
          [event.target.experience]:event.target.value
        });
  };

  render() {
    const { classes } = this.props;

    return (
      <div>
        <Typography variant="headline">Header Info</Typography>
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="name-simple">Name</InputLabel>
          <Input name="name" value={this.state.name} id="name-simple" onChange={this.handleChange}/>
        </FormControl><br></br>
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="title-simple">Title</InputLabel>
          <Input name="title" id="title-simple" value={this.state.title} onChange={this.handleChange}/>
        </FormControl><br></br>
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="email-simple">Email</InputLabel>
          <Input name="email" id="email-simple" value={this.state.email} onChange={this.handleChange}/> 
        </FormControl><br></br>
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="experience-simple">Experience</InputLabel>
          <Input name="experience" id="experience-simple" value={this.state.experience} onChange={this.handleChange}/>
        </FormControl><br></br>
      </div>
    );
  }
}

I need to pass those 4 values into another ponent

function Header(props) {
  const { classes, avatar } = props;
  return (
    <div>
              <Typography variant="headline">KASUN FERNANDO</Typography>
              <Typography variant="subheading" color="textSecondary">
              SENIOR DESIGNER-UI/UX
              </Typography>
              <Typography variant="subheading" color="textSecondary">
              [email protected]
              </Typography>
              <Typography variant="subheading" color="textSecondary">
              4+ years of experience
              </Typography>
    </div>
  );
}

In this ponent there are 4 typogrphy I need to display that state value in 4 typography

How can I do this please help me im new to React js

I have ponent like below it have 4 state values

class ComposedTextField extends React.Component {
  state = {
    name: '',
    title: '',
    email: '',
    experience: ''
  };

  handleChange = event => {
    this.setState({ 
          [event.target.name]:event.target.value,
          [event.target.title]:event.target.value,
          [event.target.email]:event.target.value,
          [event.target.experience]:event.target.value
        });
  };

  render() {
    const { classes } = this.props;

    return (
      <div>
        <Typography variant="headline">Header Info</Typography>
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="name-simple">Name</InputLabel>
          <Input name="name" value={this.state.name} id="name-simple" onChange={this.handleChange}/>
        </FormControl><br></br>
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="title-simple">Title</InputLabel>
          <Input name="title" id="title-simple" value={this.state.title} onChange={this.handleChange}/>
        </FormControl><br></br>
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="email-simple">Email</InputLabel>
          <Input name="email" id="email-simple" value={this.state.email} onChange={this.handleChange}/> 
        </FormControl><br></br>
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="experience-simple">Experience</InputLabel>
          <Input name="experience" id="experience-simple" value={this.state.experience} onChange={this.handleChange}/>
        </FormControl><br></br>
      </div>
    );
  }
}

I need to pass those 4 values into another ponent

function Header(props) {
  const { classes, avatar } = props;
  return (
    <div>
              <Typography variant="headline">KASUN FERNANDO</Typography>
              <Typography variant="subheading" color="textSecondary">
              SENIOR DESIGNER-UI/UX
              </Typography>
              <Typography variant="subheading" color="textSecondary">
              [email protected]
              </Typography>
              <Typography variant="subheading" color="textSecondary">
              4+ years of experience
              </Typography>
    </div>
  );
}

In this ponent there are 4 typogrphy I need to display that state value in 4 typography

How can I do this please help me im new to React js

Share Improve this question edited May 24, 2018 at 9:20 supra28 1,63610 silver badges17 bronze badges asked May 24, 2018 at 6:50 HemalHerathHemalHerath 1,0562 gold badges18 silver badges38 bronze badges 1
  • If the Header ponent is not related to posedTextField, you either need to store the data in a mon parent or use redux, mobx. Check stackoverflow./questions/46594900/… – Shubham Khatri Commented May 24, 2018 at 7:00
Add a ment  | 

3 Answers 3

Reset to default 9

There are a lot of non redux, mobx, flux strategies on how to manage/pass state between ponents - Props, Instance Methods, Callbacks etc. You can read this article on ponent munication. You might want to take a look at these before going with a heavy weight state management framework.

From the code given, I am assuming header is a higher level ponent. For child to parent munication you can make use of callback functions

The parent would pass a function to the child as a prop, like this:

<MyChild myFunc={this.handleChildFunc} />

And the child would call that function like so:

this.props.myFunc();

How are these two ponents related?

Do they have the same parent ponents?

If yes you can handle the state in the parent ponent and pass the handler function down to ComposedTextField as a prop which ComposedTextField calls onChange

class ParentComponent extends React.Component {
  handleChange = event => {
    this.setState({
      [event.target.name]: event.target.value,
    })
  }

  render() {
    return (
      <div>
        <HeaderComponent {...this.state} />
        <ComposedTextField onChange={this.handleChange} {...this.state}/>
      </div>
    )
  }
}

then inside your ComposedTextField you will have something like

class ComposedTextField extends React.Component {


  render() {
    const { classes } = this.props

    return (
      <div>
        <Typography variant="headline">Header Info</Typography>
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="name-simple">Name</InputLabel>
          <Input
            name="name"
            value={this.props.name}
            id="name-simple"
            onChange={this.props.onChange}
          />
        </FormControl>
        <br />
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="title-simple">Title</InputLabel>
          <Input
            name="title"
            id="title-simple"
            value={this.props.title}
            onChange={this.props.onChange}
          />
        </FormControl>
        <br />
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="email-simple">Email</InputLabel>
          <Input
            name="email"
            id="email-simple"
            value={this.props.email}
            onChange={this.props.onChange}
          />
        </FormControl>
        <br />
        <FormControl fullWidth className={classes.formControl}>
          <InputLabel htmlFor="experience-simple">Experience</InputLabel>
          <Input
            name="experience"
            id="experience-simple"
            value={this.props.experience}
            onChange={this.props.onChange}
          />
        </FormControl>
        <br />
      </div>
    )
  }
}

Your header ponent will use the props to display the set values like

function Header(props) {
  const { classes, avatar } = props
  return (
    <div>
      <Typography variant="headline">{props.name}</Typography>
      <Typography variant="subheading" color="textSecondary">
        {props.title}
      </Typography>
    </div>
  )
}

To acplish it, you should introduce some global state mechanism (Redux, MobX, ...).

handleChange - instead of setting data to the ponent's state - will dispatch actions that will write data to the global state, from which any other ponent, like Header, will be allowed to read.

发布评论

评论列表(0)

  1. 暂无评论