简体   繁体   English

如何根据这些组件中定义的某些操作在页面上呈现不同的组件?

[英]How do I render different components on a page according to some action defined inside those said components?

If I have the following App component:如果我有以下 App 组件:

function App() {
  if(test)
    return <Component1 />;
  else
    return <Component2 />;
}

But the value of test (true/false) is dependent on a button inside the two respective components.但是test的值(真/假)取决于两个各自组件内的按钮。 Clicking the button should flip the value of test (ie set to true if false and vice versa).单击按钮应翻转test的值(即,如果为false ,则设置为true ,反之亦然)。 How would I implement this?我将如何实现这一点?

You define test on App level and pass down a callback您在 App 级别定义test并传递回调

function App() {
  let [test, setTest] = useState(false)
  if(test)
    return <Component1 setTest={setTest}/>;
  else
    return <Component2 setTest={setTest}/>;
}

inside the component you can implement the flipping logic like组件内部,您可以实现翻转逻辑,例如

props.setTest(ps=>!ps);

Your parent component should should have a state and provide child components with a proper way to change that state.您的父组件应该具有 state 并为子组件提供适当的方法来更改 state。 Similar implementation as @gmoniava provided but without hooks may look like below:@gmoniava提供但没有钩子的类似实现可能如下所示:

class App extends React.Component {
  constructor(props) {
    super(props)
    this.changeState = this.changeState.bind(this);
    this.state = {
      test: false
    }
  }

  changeState() {
    this.setState({ test: !this.state.test });
  }

  render() {
    const { test } = this.state;

    return (
      <div>
        {test && <Component1 onChangeState={this.changeState} />}
        {!test && <Component2 onChangeState={this.changeState} />}
      </div>
    )
  }
}

App component contains state with test property in it. App组件包含 state,其中包含test属性。 Also it has changeState function which toggle test in state.它还具有changeState function ,在 state 中进行切换test

changeState function is passed to child components as props. changeState function 作为道具传递给子组件。 Child component calls that function inside which triggers parent function and state is changed:子组件调用 function 内部触发父 function 和 state 更改:

function Component1(props) {
  return (<button onClick={props.onChangeState}>Component 1</button>);
}

Here is demo .这是演示

This works:这有效:

function App() {
  const [test, setTest] = useState(false);

  const example = () => {
    setTest(!test);
  };

  if (!test) {
    return <Component1 example={example} />;
  } else {
    return <Component2 example={example} />;
  }
}

and inside Component1 (and Component2 ):Component1 (和Component2 )内部:

function Component1(props) {
  return <button onClick={() => props.example()}>Click</button>;
}

Passing setTest directly as prop didn't work.直接将setTest作为道具传递是行不通的。

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

相关问题 如何使用自定义菜单渲染组件? - How do I render components with custom menu? 如何在 Svelte 中使用开关渲染组件? - How do I render components with a switch in Svelte? 如何递归渲染嵌套组件? - How do I recursively render nested components? 如何使用包含一些组件名称的数组来呈现那些预定义的组件 - How to use an array containing some component names to render those predefined components 如何根据页面组件动态地 position 按钮? - How can I position button dynamatically according to page components? 如何让路由器视图在另一个组件内渲染组件? - How do I get router-view to render components inside another component? 如何使用 react-router-dom v6 渲染具有不同布局/元素的组件 - How do I render components with different layouts/elements using react-router-dom v6 如何在onClick上呈现不同的组件? (组件应由最新点击的处理程序替换。) - How do I render different components onClick? (Component should be replaced by the newest clicked handler.) 如何在 React 的初始加载时有条件地渲染不同的组件? - How can I conditionally render different components on initial load in React? 如何在 reactjs 的不同占位符中渲染不同的组件? - How to render different components in different placeholders in reactjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM