question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cannot pass props to child component

See original GitHub issue

Hi, I’m trying to implement your mobx-react-form package by myself but I have following issue. I would like to pass props for example form={form.$(‘product’)} defined in NewProductForm.jsx into the component Input defined in Input.jsx.

I checked what I get when I write console.log(props.form) in Input component and it tells me that form.$(‘product’) is empty array and is rendered just this tag <input type="text" />. Probably I’m not passing props correctly but I dont how I can do it in another way.

fields definition in FormFieldConst.jsx

export default {
  fields: {
    product: {
      name: 'productname',
      label: 'productname',
      placeholder: 'write product name',
      rules: 'required|string|between:5,65',
    }
    ....
}

ValidationForm.jsx

const plugins = { dvr: validatorjs };
class ValidateForm extends MobxReactForm {
  onSuccess(form) {
    console.log(form.values());
  }
  onError(form) {
    console.log(form.errors());
  }
}
const validateform = new ValidateForm({ fields, plugins });
export default { validateform };

NewProductForm.jsx

import form from '../components/ValidationForm';

const NewProductForm = () => (
  <form className={styles.newform} onSubmit={form.onSubmit}>
    <Input className="form-input" type="text" form={form.$('product')} />
    <Input className="form-input" type="text" form={form.$('inventory')} />
    <Input className="form-input" type="text" form={form.$('type')} />
    <Input className="form-input" type="text" form={form.$('vendor')} />
    <Button classBtnName="form-button" BtnText="Send Form" />
  </form>
);

export default observer(NewProductForm);

Input.jsx

const Input = props => (
  <div className={props.className}>
    <label htmlFor={props.form.label} >
    </label>
      <input type={props.type} name={props.form.name} value={props.form.name} placeholder={props.form.placeholder} onChange={props.form.sync} />
  </div>
  );

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
foxhound87commented, Dec 8, 2016

Now you can use placeholder. The docs is updated too.

0reactions
foxhound87commented, Dec 12, 2016

About extending the forms and fields, I will create a more detailed guide for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can I not pass props to child component React?
I currently have code for the date to update and pass to a date component, but now working on a stopwatch and having...
Read more >
Passing Props to a Component - React Docs
React components use props to communicate with each other. Every parent component can pass some information to its child components by giving them...
Read more >
How to Use Props to Pass Data to Child Components in React.js
The last step is to access the props in the child component. In function components, we access the props through the first function...
Read more >
How to pass props to a component in React
We cannot pass props from a child component to a parent component. Props always move from top to bottom in the component hierarchy....
Read more >
How to use Props in React - Robin Wieruch
As said, there is no way passing props from a child to a parent component. But you can always pass functions from parent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found